Home C-tutorials C String Reverse
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

C String Reverse

Advertisement
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];  
st[i]=st[strlen(st)-i-1]; 
st[strlen(st)-i-1]=c;

Statements shown above will reverse the string.

Here is the code:

 

StringReverse.C

#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);
  reverse(0);
  printf("\nReversed string is: %s", st);
  getch();
}
int reverse(int i) {
  if (i<(strlen(st)/2)) {
  char c;
  c= st[i];
  st[i]=st[strlen(st)-i-1];
  st[strlen(st)-i-1]=c;
  }
  return 0;
}

Output will be displayed as:

STRING~1.EXE

Download Source Code:

Advertisements

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

Ask Questions?    Discuss: C String Reverse   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
k.vijay
June 22, 2011
please easy to explain

i could not understand
lavanya
June 24, 2011
Explanation

string handling functions are not working in LINUX operating System.while compiling its throwing an error.so i need explanation for that and clrscr() function works in c are not?
Sidharth
August 15, 2011
hi here is a small correction

replace while with if condition, it works well otherwise it gives output as dello worlh
Gaurav
September 7, 2011
REVERSE A STRING

A good and understandable program. I like it.
nitesh
September 29, 2011
correction

change if loop to for loop; other wise it wont work totally.only the first and last are reversed ,other will remain same if u use if.
Megatron
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'; }
sudhir
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
Firdaus
November 24, 2011
Thanks for the tuts

Thanks! This is help ;) Cheers!
uday
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; }
jfr
December 12, 2011
Missing line in html version of code.

You are missing the recursive call.
Pankaj
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
c++

nice one...............
janeperez
November 26, 2012
stringreverse

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

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