سؤال

I've read a lot about this, but I can not understand, I want to change a character of strings, for example.

Let's think that I have the following string:

char a [] = "stackoverflow" 

Then I want to add 5 more letters at the end, or remove 2 more letter, etc... getting the string like this:

char b [] = "stackoverflowstack"
char b [] = "stackoverfl"

How can I do this, someone explain to me please, thanks in advance...

هل كانت مفيدة؟

المحلول

You can use dynamic memory allocation with C library calls such as malloc. This requires that you have heap memory available. I will not discuss this further as there are plenty of references available.

If this is not an option, you'll need to use strncpy and create arrays large enough to store the partial results.

For instance, in the example you chose:

const char* stackOverflow[] = "stackoverflow";
char a[20];  // Enough room for the largest possible string.

strncpy(a, stackOverflow, sizeof(a));
strncpy(a, "stackoverflowstack", sizeof(a)); // Copy null char as well
strncpy(a, "stackoverfl", sizeof(a)); // Copy null char as well

نصائح أخرى

The simplest approach is to pre-allocate enough memory in your array to hold the longest string you expect to store in it.

An array is a region of storage; it has a size (number of elements) that's fixed at the point where it's created. You can't expand an existing array -- though you can use realloc to replace an array by a longer one.

A string is a sequence of characters up to and including a terminating '\0' null character.

An array may contain a string -- and the length of the string is not the same thing as the size of the array.

Note that the length of a string does not include the terminating '\0'; `"hello" has a length of 5 and a size of 6.

Your declaration:

char a[] = "stackoverflow";

causes the compiler to create a with a size just big enough to hold the string used to initialize it. It's equivalent to:

char a[14] = "stackoverflow"; /* 13 characters + 1 for the '\0' */

You can shorten the string stored in the array a, but it can't hold a string with a length greater than 13:

strcpy(a, "STACK");

After this, the array a is still 14 characters in size (that never changes), but its contents are now:

"STACK\0verflow\0"

but anything after the first '\0' will probably be ignored.

If you anticipate storing a longer string, you can create it with a larger size:

char a[100] = "stackoverflow";
strcat(a, " is fun!";

This approach can be both wasteful (the array is likely to be bigger than it needs to be at the moment) and error-prone (it's difficult to avoid overflowing the array bounds by storing an overly long string in it). But if you're sufficiently careful, it can be good enough.

There are some string manipulation functions that make it easier to avoid overflowing your array. strncat is a safer version of strcat that lets you specify the size of the target array. There are also some non-standard functions like strlcpy and strlcat.

(strncpy, however, is not recommended.)

Other approaches are possible. For example, you can dynamically allocate memory space using malloc(), and reallocate it (replacing your array by a larger or smaller one) using realloc. You still have to be careful not to read or write past the end of an array -- and you have to keep track of the array size yourself. It's a more flexible approach, but it imposes more overhead, both on the performance of your program and on you as a programmer.

C (unlike C++, BTW) doesn't have a simple mechanism for managing strings of varying lengths. The C string facilities, in some cases, are what higher-level mechanisms are built on top of.

Short answer is no.

Somewhat longer answer: these strings have a limited length and you can't expand them. But you can create a longer string and then concatenate them there:

char* new_string = (char*) malloc((strlen(a) + strlen(b) + 1) * sizeof(char));
strcpy(new_string, a);
strcat(new_string, b);
printf("%s", new_string);

malloc(size_t len) allocates a block of memory len bytes long. Note that I allocate memory one byte longer than required: C strings terminate with a null-byte and it is not counted with strlen, yet it is still required.

strcpy(char * dst, const char * src) copies a string from location src to location dst. You may and probably will get some kind of bad behavior when length of dst is less than length of src.

strcat(char * dst, const char * src) adds a string from src to the end of dst.

You may use static memory allocation (like char new_string[20]) instead of malloc, although it somewhat limits your string length (obviously) and may lead to buffer overruns: be careful with it.

Further reading: Wikibooks tutorial on string handling.

The only way to resize a string is to dynamically allocate it with malloc() initially.

You can not resize anything that is statically declared.

char * a = malloc(14);

a = realloc(a, 20);

(relevant checking of return value omitted)

Of coure, you could just statically declare an array large enough to hold the largest string (plus null terminator):

char a[20];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top