C Program to Reverse a String | C Program Without Using String Functions
1. Using inbuilt function strrev()
In this program, we will use inbuilt library function called as strrev() which reverses the string and print that string on the screen.
Output:

2. Reversing a string without using a function (using for loop)
The first program was very simple because we use library function for reversing a string. But how to reverse a string without using the function.
In this program, you will learn how to reverse a string without using a function.
Output:

Explanation:
In the above output, you can observe that str[]=Hello and at the initial stage the value of i is 0 i.e i=0. So, len=strlen(Str)=5.Here strlen is the inbuilt function to find the length of the string i.e 5.
1st Iteration: for(i=len-1;i>=0;i–) i.e for(i=5-1;4>=0;4–).Here the condition (4>=0) is true.Therefore, RevStr[j++]=Str[i] i.e RevStr[0]=Str[4]=o.
2nd Iteration: for(i=len-1;i>=0;i–) i.e for(i=4-1;3>=0;3–).Here the condition (3>=0) is true.Therefore, RevStr[j++]=Str[i] i.e RevStr[1]=Str[3]=l.
3rd Iteration: for(i=len-1;i>=0;i–) i.e for(i=3-1;2>=0;2–).Here the condition (2>=0) is true.Therefore, RevStr[j++]=Str[i] i.e RevStr[2]=Str[2]=l.
4th Iteration: for(i=len-1;i>=0;i–) i.e for(i=2-1;1>=0;1–).Here the condition (1>=0) is true.Therefore, RevStr[j++]=Str[i] i.e RevStr[3]=Str[1]=e.
5th Iteration: for(i=len-1;i>=0;i–) i.e for(i=1-1;0>=0;0–).Here the condition (0>=0) is true.Therefore, RevStr[j++]=Str[i] i.e RevStr[4]=Str[0]=H.
6th Iteration: now the for loop will stop executing and the program will exit.
3. Without storing in a separate array
In the above program, we were storing the reverse string in a separate array. In this program, we will not store the reverse string in a separate array.
Output:

4. Reversing a string with the concept of Swapping
In this program, we will use the concept of swapping. The temporary variable temp is declared in the program.
Output:

5. C program to reverse a string using the function
In this program, we will declare the user-defined function to reverse a string.
Output:

In the above program the user-defined function is reverse_String.
6. C Program to Reverse a String using Recursion
Recursion means calling a function again and again till the condition get false.
Output:

7. C program to reverse a String Using Pointers
In this program, we will reverse the string with the help of pointers.
Output:

Post a Comment