C Programming
- Print integer
- Add & subtract
- Odd & even
- Multiply & divide
- Check vowels
- Letter grade program
- Leap year program
- Sum of digits
- Prime numbers
- Sum of numbers
- Find factorial
- Fibonacci series
- Fibonacci triangle
- Number triangle
- Alphabet triangle
- Armstrong numbers
- Palindrome numbers
- Swap number
- Reverse number
- Decimal to binary
- Assembly code in C
- Matrix multiplication
- HCF & LCM
- nCr & nPr
- Print pattern
- Floyd’s triangle
- Pascal triangle
- Count digits
- Strong numbers
- Perfect numbers
- Sum of natural number
- Binary to decimal
- Decimal to octal
- Octal to decimal
- Binary to octal
- Octal to binary
- Add using pointer
- Bubble sort
- Insertion sort
- Selection sort
- Quick sort
- Binary search
- Linear search
- Largest element of array
- Smallest element of array
- Reverse Array
- Variable size & limit
- ASCII value of character
- Sum of array elements
- Number of element in array
- Merge two array
- Insert element in array
- Delete element from array
- Add two matrix
- Transpose matrix
- Print string
- Reverse string
- Delete vowels
- Sort string
- Remove spaces
- Swap string
- Random numbers
- Print date & time
- Print IP address
- Shut down computer
C program to reverse string
In this guide we will learn how to reverse string in C. We will take a string as input and then reverse it by library function, without library function, recursion or pointers.
Assume the user gives the string ‘programming‘, then we will make it ‘gnimmargorp‘ by our written C program. Let’s see the easiest solution to this.
Reverse string using strrev() library function
strrev() is a library function defined inside the string.h header file which can reverse the given string. We must have to include the header file when working string function like strrev().
#include <stdio.h>
#include <string.h>
int main(){
char main_string[200];
printf("Enter a string here to reverse it : \n");
gets(main_string);
strrev(main_string);
printf("\nString after reversal is : \n%s\n", main_string);
return 0;
}
Output of reverse string program:

String reverse program without strrev()
// reverse string program without library function
#include <stdio.h>
int main(){
char main_string[100], rev_string[100];
int counter = 0, start, finish;
printf("Input any string to reverse it : \n");
gets(main_string);
while(main_string[counter] != '\0'){
counter++;
}
finish = counter - 1;
for(start = 0; start < counter; start++){
rev_string[start] = main_string[finish];
finish--;
}
rev_string[start] = '\0';
printf("\nReverse string is : \n%s\n", rev_string);
return 0;
}
Output of program:
Input any string to reverse it :
string reversal by CP
Reverse string is :
PC yb lasrever gnirts
You can also use function to reverse a string. Try this using function in your own.
Reverse string using pointer in C
// reverse string program using pointer
#include<stdio.h>
int len_finder(char *p){ // find string length by function
int i = 0;
while(*(p + i) != '\0'){
i++;
}
return i;
}
void str_rev_funct(char *strg){ // string reverse function
int strln, k;
char *s, *e, t;
strln = len_finder(strg);
s = strg;
e = strg;
for (k = 0; k < strln - 1; k++){
e++;
}
for (k = 0; k < strln / 2; k++){
t = *e;
*e = *s;
*s = t;
s++;
e--;
}
}
int main(){
char main_string[200];
printf("Enter your main string to reverse it : \n");
gets(main_string);
str_rev_funct(main_string);
printf("\nYour string after reversing is : \n%s\n", main_string);
return 0;
}
Reverse string program output:
Enter your main string to reverse it :
reverse string program
Your string after reversing is :
margorp gnirts esrever
Previous page:Â Print string
Next page:Â Delete vowels
Recommended for you: