The main problem comes when we swap strings of unknown lengths. Lets say we have two strings assigned to two variables say a, b. Now if I use a temporary string say c, and use strcpy(c,a), then use strcpy(a,b) finally using strcpy(b,c).

It works when we have strings of equal length, but not with strings of different lengths. Is there any method to swap two strings of unknown lengths?

1 Answer 1


Let's say a is allocated for 4 and holds "red", and b is allocated for 5 and holds "blue". a cannot hold "blue". You could perhaps use realloc. Program would have to allocate c for the size of the larger string, and the smaller string would need to be reallocated to accommodate the larger string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .