In the interview generally, Interviewer asked the question to reverse a string without the using the c library function or maybe they can mention more condition, it totally depends on the interviewer.
In this article, I will discuss several techniques to reverse a string. I hope you guys like it. Here I am assuming you guys already familiar with string but if you don’t have any idea about the string then don’t need to worry. I am giving here the small introduction of the string.
A string is the collection of characters and it terminate always with a null character means that every string contains a null character at the end of the string.
Example:
char *pszData = “aticle”;
In above example, pszData is the pointer to the string. All characters of the string are stored in a contiguous memory and consist a null character in the last of the string.
See the below table:
character | ‘a’ | ‘t’ | ‘i’ | ‘c’ | ‘l’ | ‘e’ | ‘\0’ |
Address | 0x00 | 0x01 | 0x02 | 0x03 | 0x04 | 0x05 | 0x06 |
Here we will reverse a string using two methods, iterative and recursive.
Iterative method
An iterative method is the simplest way to reverse a string in c. In this article, I am discussing few methods to reverse a string in c.
Algorithm:
- Calculate the length (Len) of string.
- Initialize the indexes of the array.
Start = 0, End = Len-1 - In a loop, swap the value of pszData[Start] with pszData[End].
- Change the indexes’ of the array as follows.
Start = start +1; End = end – 1
Reverse a string using the temporary variable:
#include <stdio.h> #include <stdlib.h> int main() { char acData[100]= {0}, Temp = 0; int iLoop =0, iLen = 0; printf("\nEnter the string :"); gets(acData); // calculate length of string while(acData[iLen++] != '\0'); //Remove the null character iLen--; //Array index start from 0 to (length -1) iLen--; while (iLoop < iLen) { Temp = acData[iLoop]; acData[iLoop] = acData[iLen]; acData[iLen] = Temp; iLoop++; iLen--; } printf("\n\nReverse string is : %s\n\n",acData); return 0; }
OutPut:
Explanation of the program:
Firstly find the length of the string
while(acData[iLen++] != ‘\0’);
we know that string contains a null character, so remove the null character.
iLen–;
Let suppose “ABCD” is the string, so when we try to calculate the length of the string using the above expression then iLen also counts the null character. So here I am reducing the iLen to exclude the null character.
while(acData[iLen++] != ‘\0’);
iLen = 5;
iLen–;
iLen = 4;
We know that a string is a character array and it has a range between 0 to (iLen-1);
iLen–; // So for the index of array
iLen = 3;
Now we have the starting and ending location of the character in the string.
iLoop =0;
iLen =3;
‘iLoop’ is the first positioned character and ‘iLen’ is positioned off the last character. Now we are swapping characters at positions ‘iLoop’ and ‘iLen’. After swapping the characters we will increment the value of ‘iLoop’ and decrement the value of ‘iLen’.
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.
Reverse a string using an EX_OR operator
This method is similar to the above program, in which we just use EX-OR operator to swap the character beside the third variable.
#include <stdio.h> #include <stdlib.h> int main() { char acData[100]= {0}; int iLoop =0, iLen = 0; printf("\nEnter the string :"); gets(acData); // calculate length of string while(acData[iLen++] != '\0'); //Remove the null character iLen--; //Array index start from 0 to (length -1) iLen--; while (iLoop < iLen) { acData[iLoop]= acData[iLoop] ^ acData[iLen]; acData[iLen]= acData[iLoop] ^ acData[iLen]; acData[iLoop]= acData[iLoop] ^ acData[iLen]; iLoop++; iLen--; } printf("\n\nReverse string is : %s\n\n",acData); return 0; }
OutPut:
Reverse a string using an extra buffer.
This is a very simple method to reverse a string. In this method, we use an extra buffer to store the reverse string.
#include <stdio.h> #include <stdlib.h> int main() { char acData[100]= {0}; char acReverse[100]= {0}; int iLoop =0, iLen = 0; printf("\nEnter the string :"); gets(acData); // calculate length of string while(acData[iLen++] != '\0'); //exclude null character --iLen; while (iLen >= 0) { acReverse[iLoop++]= acData[--iLen]; } printf("\n\nReverse string is : %s\n\n",acReverse); return 0; }
OutPut:
Reverse a string using two pointers
Here we are using two pointers for the character swapping.
#include <stdio.h> #include <stdlib.h> int main() { char acData[100]= {0}; char *pcStart = NULL; char *pcEnd = NULL; int iLoop =0, iLen = 0; printf("\nEnter the string :"); gets(acData); //Pointer point to the address of first character pcStart = acData; // calculate length of string while(acData[iLen++] != '\0'); //Remove the null character iLen--; pcEnd = (pcStart + iLen-1); while (iLoop < iLen/2) { *pcStart = (*pcStart) ^ (*pcEnd); *pcEnd = (*pcStart) ^ (*pcEnd); *pcStart = (*pcStart) ^ (*pcEnd); pcStart++; pcEnd--; iLoop++; } printf("\n\nReverse string is : %s\n\n",acData); return 0; }
OutPut:
Reverse a string using the macro
This method is also similar to the above-described method. In which we create a macro and pass the starting and ending address of the string and performed the swap operation.
#include <stdio.h> #include <stdlib.h> #define SWAP_CHARACTER(a,b) do { \ (*a)^=(*b); \ (*b)^=(*a);\ (*a)^=(*b);\ a++; \ b--; \ }while(0); int main() { char acData[100]= {0}; char *pcStart = NULL; char *pcEnd = NULL; int iLoop =0, iLen = 0; printf("\nEnter the string :"); gets(acData); //Pointer point to the address of first character pcStart = acData; //calculate length of string while(acData[iLen++] != '\0'); //Remove the null character iLen--; pcEnd = (pcStart + iLen-1); while (iLoop < iLen/2) { SWAP_CHARACTER (pcStart,pcEnd); iLoop++; } printf("\n\nReverse string is : %s\n\n",acData); return 0; }
Output:
Reversing a string using a single pointer:
#include <stdio.h> #include <stdlib.h> int main() { char acData[100]= {0}; char *pcReverse = NULL; int iLoop =0, iLen = 0; printf("\nEnter the string :"); gets(acData); //Pointer point to the address of first character pcReverse = acData; // calculate length of string while(acData[iLen++] != '\0'); //Remove the null character iLen--; pcReverse = (acData + iLen-1); while (iLoop < iLen/2) { acData[iLoop] = acData[iLoop] ^ (*pcReverse); *pcReverse = acData[iLoop] ^ (*pcReverse); acData[iLoop] = acData[iLoop] ^ (*pcReverse); pcReverse--; iLoop++; } printf("\n\nReverse string is : %s\n\n",acData); return 0; }
OutPut:
Recursive Way to reverse a string
- Calculate the length (Len) of string.
- Initialize the indexes of the array.
Start = 0, End = Len-1 - swap the value of pszData[Start] with pszData[End].
- Change the indexes’ of an array as below and Recursively call reverse function for the rest array.
Start = start +1; End = end – 1
Method 1:
#include <stdio.h> #include <stdlib.h> //recursive function int StringRev(char *pszInputData, unsigned int Start, unsigned int End) { if(Start >= End) { return 1; } // swap the data *(pszInputData + Start) = *(pszInputData + Start) ^ *(pszInputData + End); *(pszInputData + End) = *(pszInputData + Start) ^ *(pszInputData + End); *(pszInputData + Start) = *(pszInputData + Start) ^ *(pszInputData + End); //function called repeatedly StringRev(pszInputData,Start+1, End-1); return 0; } int main() { char acData[100]= {0}; int iLen = 0; unsigned int Start=0; printf("\nEnter the string :"); gets(acData); // calculate length of string while(acData[iLen++] != '\0'); //Remove the null character iLen--; //Find array last index iLen--; StringRev(acData,Start, iLen); printf("\n\nReverse string is : %s\n\n",acData); return 0; }
OutPut:
Method 2:
In this method, a reverse function does not insert the null character in the last of the string. So before using this method you have to insert the null character manually in the last of the string.
#include <stdio.h> #include <stdlib.h> int StringRev(char *pszInputData, char *pszReverseData) { static int i = 0; if(*pszInputData != '\0') { StringRev((pszInputData + 1),pszReverseData); pszReverseData[i++]= *pszInputData; } return 0; } int main() { char acData[100]= {0}; char acReverseData[100] = {0}; printf("\nEnter the string :"); gets(acData); StringRev(acData,acReverseData); printf("\n\nReverse string is : %s\n\n",acReverseData); return 0; }
OutPut: