append chars to get a string
I have the following code in C:
char *str = "Hello World";
char *s = malloc(strlen(str));
int i =0;
for(;i<strlen(str)-5;i++)
{
s += *(str+i);
}
printf(s);
It shows nothing. What I want is to get the substring of str
stored in s
.
In Java I would do the follow开发者_开发知识库ing:
String str = "Hello World";
String s="";
for(int i=0;i<str.length()-5; i++)
s+=str[i];
System.out.println(s);
Or instead use the substring method. As String s = str.substring(1,2);
for example.
How can I achieve it?
Use the strcpy
function.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[] )
{
char *str = "Hello World";
size_t length = strlen(str);
char *s = (char*)malloc(sizeof(char) * (length + 1));
strcpy(s, str);
s[length] = '\0'; // makes sure it's NUL terminated
printf("%s", s);
free(s);
return 0;
}
When allocating the destination buffer, pay attention to the fact that strings are terminated by the NUL
character.
To only copy a substring, use strncpy
:
strncpy(s, str + 6, strlen(str) - 6);
will just copy "World" into s
.
In any case, make sure your C strings are NUL
terminated before using functions like printf
.
See also strcat
and strncat
. And well, familiarize yourself with C arrays and pointers.
Others have said how to correctly solve this problem (strcat) but the trick is to think about types. C doesn't do ANYTHING magical for you. s is a char*. what happens when you add 3 to a char*? you get another char* that is pointing 3 characters farther down the line. Adding 'a' to a char* is the same as adding 97 (ascii of 'a') to the pointer, and thus pointing to another character far down the array...
I hope that explains what was happening...
You can't use the +=
operator on a char*
object - it's just a pointer to char
. Instead, you'll want to use strncpy
, and pass it a pre-allocated buffer, plus a pointer to where in the string you want the copying to begin, and the number of characters you want to copy.
C strings are pointers to characters in memory, not objects like in other languages. I suggest you Google for an explanation of strings (this one, for example) and then look at the strcpy
function defined in string.h
.
For substrings in general where you want to omit characters from both the beginning and the end, you should use strncpy or strncat. (Be careful, though: strncpy doesn't necessarily NUL-terminate.)
Use strncpy()
:
char *str = "Hello World";
size_t len = strlen(str) - 5;
char *s = malloc(len + 1); // +1 for nul terminator
if (s)
{
strncpy(s, str, len); // copies strlen(str)-5 characters from str to s
s[len] = 0; // add nul terminator
}
A somewhat more general function:
/**
* Return a substring of _len_ characters starting at position _start_
* substring("Hello, World", 2, 3) == "llo"
*/
char *substring(char *str, size_t start, size_t len)
{
char *s = malloc(len + 1);
if (s)
{
strncpy(s, str+start, len);
s[len] = 0;
}
return s;
}
Add sanity checks as needed (e.g., start < strlen(str), len <= strlen(str) - start, etc.).
You need to allocate memory for the string that will hold the substring. Maybe something like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LESS 4
#define MAX 10 /*Keep it atleast strlen(str) - LESS + 1*/
int main(int argc, char *argv[] )
{
char *str = "Hello World";
char s[MAX]; /* Declare the array to hold the substring.*/
int i =0,j=0; /* i is index into original string and j in the result string. */
for(;i<strlen(str)-LESS;i++)
{
s[j++] = *(str+i);
}
s[j] = '\0'; /* Terminate the result string.*/
printf("%s\n",s); /* Output: Hello W */
return 0;
}
You can do what you're asking to do with the following changes to your code:
int main (int argc, char *argv[])
{
char *str = "Hello World";
char *s = malloc(strlen(str));
int i = 0;
for(;i<strlen(str)-5;i++)
{
s[i] = *(str+i);
}
s[i] = 0;
printf(s);
return 0;
}
'+' is not a stribg concatenation operator in C
精彩评论