This program Reverse string in C entered by the user. For example if a user enters a string “reverse me” then on reversing the string will be “em esrever”. We show you four different methods to reverse string the first one uses strrev library function of string.h header file, second without using strrev and in third we make our own function to reverse string using pointers, reverse string using recursion and Reverse words in string. If you are using first method then you must include string.h in your program.

[ad type=”banner”]

C programming code

/* String reverse in c*/

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20char%20arr%5B100%5D%3B%0A%20%0A%20%20%20printf(%22Enter%20a%20string%20to%20reverse%5Cn%22)%3B%0A%20%20%20gets(arr)%3B%0A%20%0A%20%20%20strrev(arr)%3B%0A%20%0A%20%20%20printf(%22Reverse%20of%20entered%20string%20is%20%5Cn%25s%5Cn%22%2Carr)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output of program:

Reverse string in C

C program to reverse string without using function

Below code does not uses strrev library function to Reverse string in C. First we calculate the length of string using strlen function and then assigns characters of input string in reverse order to create a new string using a for loop. You can also calculate length of string without using strlen. We use comma operator in for loop to initialize multiple variables and increment/decrement variables.

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20char%20s%5B100%5D%2C%20r%5B100%5D%3B%0A%20%20%20int%20n%2C%20c%2C%20d%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(s)%3B%0A%20%0A%20%20%20n%20%3D%20strlen(s)%3B%0A%20%0A%20%20%20for%20(c%20%3D%20n%20-%201%2C%20d%20%3D%200%3B%20c%20%3E%3D%200%3B%20c–%2C%20d%2B%2B)%0A%20%20%20%20%20%20r%5Bd%5D%20%3D%20s%5Bc%5D%3B%0A%20%0A%20%20%20r%5Bd%5D%20%3D%20’%5C0’%3B%0A%20%0A%20%20%20printf(%22%25s%5Cn%22%2C%20r)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

C program to reverse a string using pointers

Now we will invert string using pointers or without using library function strrev.

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0Aint%20string_length(char*)%3B%0Avoid%20reverse(char*)%3B%0A%20%0Amain()%20%0A%7B%0A%20%20%20char%20string%5B100%5D%3B%0A%20%0A%20%20%20printf(%22Enter%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20reverse(string)%3B%0A%20%0A%20%20%20printf(%22Reverse%20of%20entered%20string%20is%20%5C%22%25s%5C%22.%5Cn%22%2C%20string)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Avoid%20reverse(char%20*string)%20%0A%7B%0A%20%20%20int%20length%2C%20c%3B%0A%20%20%20char%20*begin%2C%20*end%2C%20temp%3B%0A%20%0A%20%20%20length%20%3D%20string_length(string)%3B%0A%20%20%20begin%20%20%3D%20string%3B%0A%20%20%20end%20%20%20%20%3D%20string%3B%0A%20%0A%20%20%20for%20(c%20%3D%200%3B%20c%20%3C%20length%20-%201%3B%20c%2B%2B)%0A%20%20%20%20%20%20end%2B%2B%3B%0A%20%0A%20%20%20for%20(c%20%3D%200%3B%20c%20%3C%20length%2F2%3B%20c%2B%2B)%0A%20%20%20%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20temp%20%20%20%3D%20*end%3B%0A%20%20%20%20%20%20*end%20%20%20%3D%20*begin%3B%0A%20%20%20%20%20%20*begin%20%3D%20temp%3B%0A%20%0A%20%20%20%20%20%20begin%2B%2B%3B%0A%20%20%20%20%20%20end–%3B%0A%20%20%20%7D%0A%7D%0A%20%0Aint%20string_length(char%20*pointer)%0A%7B%0A%20%20%20int%20c%20%3D%200%3B%0A%20%0A%20%20%20while(%20*(pointer%20%2B%20c)%20!%3D%20’%5C0’%20)%0A%20%20%20%20%20%20c%2B%2B%3B%0A%20%0A%20%20%20return%20c%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

program to reverse string in C using recursion

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Avoid%20reverse(char*%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%0A%7B%0A%20%20%20char%20a%5B100%5D%3B%0A%20%0A%20%20%20gets(a)%3B%0A%20%0A%20%20%20reverse(a%2C%200%2C%20strlen(a)-1)%3B%0A%20%0A%20%20%20printf(%22%25s%5Cn%22%2Ca)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Avoid%20reverse(char%20*x%2C%20int%20begin%2C%20int%20end)%0A%7B%0A%20%20%20char%20c%3B%0A%20%0A%20%20%20if%20(begin%20%3E%3D%20end)%0A%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20c%20%20%20%20%20%20%20%20%20%20%3D%20*(x%2Bbegin)%3B%0A%20%20%20*(x%2Bbegin)%20%3D%20*(x%2Bend)%3B%0A%20%20%20*(x%2Bend)%20%20%20%3D%20c%3B%0A%20%0A%20%20%20reverse(x%2C%20%2B%2Bbegin%2C%20–end)%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

In recursion method we swap characters at the begin and at the end and then move towards the middle of the string. This method is inefficient due to repeated function calls but useful in practicing recursion.

Categorized in: