Let us consider the below program.
#include<stdio.h> void swap(char *str1, char *str2) { char *temp = str1; str1 = str2; str2 = temp; } int main() { char *str1 = "geeks"; char *str2 = "forgeeks"; swap(str1, str2); printf("str1 is %s, str2 is %s", str1, str2); getchar(); return 0; } |
Output of the program is str1 is geeks, str2 is forgeeks. So the above swap() function doesn’t swap strings. The function just changes local pointer variables and the changes are not reflected outside the function.
Let us see the correct ways for swapping strings:
Method 1(Swap Pointers)
If you are using character pointer for strings (not arrays) then change str1 and str2 to point each other’s data. i.e., swap pointers. In a function, if we want to change a pointer (and obviously we want changes to be reflected outside the function) then we need to pass a pointer to the pointer.
#include<stdio.h> /* Swaps strings by swapping pointers */ void swap1(char **str1_ptr, char **str2_ptr) { char *temp = *str1_ptr; *str1_ptr = *str2_ptr; *str2_ptr = temp; } int main() { char *str1 = "geeks"; char *str2 = "forgeeks"; swap1(&str1;, &str2;); printf("str1 is %s, str2 is %s", str1, str2); getchar(); return 0; } |
This method cannot be applied if strings are stored using character arrays.
Method 2(Swap Data)
If you are using character arrays to store strings then preferred way is to swap the data of both arrays.
#include<stdio.h> #include<string.h> #include<stdlib.h> /* Swaps strings by swapping data*/void swap2(char *str1, char *str2) { char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char)); strcpy(temp, str1); strcpy(str1, str2); strcpy(str2, temp); free(temp); } int main() { char str1[10] = "geeks"; char str2[10] = "forgeeks"; swap2(str1, str2); printf("str1 is %s, str2 is %s", str1, str2); getchar(); return 0; } |
This method cannot be applied for strings stored in read-only block of memory.
Please write comments if you find anything incorrect in the above article, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Swap the first and last character of a string in Java
- Swap two variables in one line
- Count of strings that become equal to one of the two strings after one removal
- Why "&" is not used for strings in scanf() function?
- Converting Strings to Numbers in C/C++
- How to convert C style strings to std::string and vice versa?
- How to write long strings in Multi-lines C/C++?
- Program to check if two strings are same or not
- Check whether an array of strings can correspond to a particular number X
- Storage for Strings in C
- Multiply N complex numbers given as strings
- Build a DFA to accept Binary strings that starts or ends with "01"
- How to call function within function in C or C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?

