Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home C-tutorials C String Reverse

Related Tutorials


Share on Google+Share on Google+

C String Reverse

In this section, you will study how to reverse a string. You can see in the given example, a recursive function reverse(int i) is created.

C String Reverse


In this section, you will study how to reverse a string. You can see in the given example, a recursive function reverse(int i) is created. Inside the method, we are calculating the length of a specified string using a library function strlen(). Then string st[i] and st[strlen(st)-i-1] are swapped as: c= st[i];  

Statements shown above will reverse the string.

Here is the code:



#include <stdio.h>
#include <conio.h>
#include <string.h>
int reverse(int i);
char st[]="Hello World";
void main() {
  printf("\nThe string is: %s", st);
  printf("\nReversed string is: %s", st);
int reverse(int i) {
  if (i<(strlen(st)/2)) {
  char c;
  c= st[i];
  return 0;

Output will be displayed as:


Download Source Code:


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 6, 2009

Related Tutorials

Discuss: C String Reverse   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 2, 2011
ANother method :P

#include<stdio.h> #include<conio.h> void rev(char *,char *,int ); int main() { char ch[10],r[10]; scanf("%s",ch); rev(ch,r,strlen(ch)); printf("%s ",r); getch(); return 0; } void rev(char *p,char *q,int n) { //static int i=0; if((n--)>0) { *(q++)=*(p+n); rev(p,q,n); } else *q='\0'; }
October 13, 2011
reversing a string

the program works fine. But for a beginner why it void main ? where the new function call has to be sent. what are the called parameters. that too without using built in function it is bit clumsy to understand. please show it in algorithmic way first and then give the solution please, thanks, / sudhir
November 24, 2011
Thanks for the tuts

Thanks! This is help ;) Cheers!
December 4, 2011
Correct the above code

#include <stdio.h> #include <string.h> int reverse(int i); char st[]="Hello World"; main() { printf("\nThe string is: %s", st); reverse(0); printf("\nReversed string is: %s", st); } int reverse(int i) { while (i<(strlen(st)/2)) { char c; c= st[i]; st[i]=st[strlen(st)-i-1]; st[strlen(st)-i-1]=c; i++; } return 0; }
December 12, 2011
Missing line in html version of code.

You are missing the recursive call.
April 5, 2012
wrong code

I have tried this code,but the result is not correct.This code is not perfect.Make it correct
niyati gandhi
July 29, 2012

nice one...............
November 26, 2012

tnx for this useful code :D it help's me a lot :*
June 24, 2013
easy but hard

hi it's understandable but little bit tough.......