Program to reverse the string

12 months ago Admin 0

Reverse the string

This article illustrates the concept of how to reverse the string in c/c++.

reverse the string

Here is the source code and output of both the programs.

C Programming language

Program


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
 char str[50],rev[50];
 int len,i;
 int j=0;
 clrscr();
 printf("Enter the string:- ");
 gets(str);
 len=strlen(str);
 for(i=len-1;i>=0;i--)
  {
   rev[j++]=str[i];
  }
 rev[i]='\0';
 printf("Reverse of the string is:- %s",rev);
 getch();
}

After successfully compiling and executing the above C program,output is:-

reverse the string


C++ Programming language

Program


#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
 clrscr();
 char str[50],rev[50];
 int len,i;
 int j=0;
 cout<<"Enter the string:- ";
 cin>>str;
 len=strlen(str);
 for(i=len-1;i>=0;i--)
  {
   rev[j++]=str[i];
  }
 rev[i]='\0';
 cout<<"Reverse of the string is:- "<<rev;
 getch();
}

After successfully compiling and executing the above C++ program,output is:-

reverse the string


Comment below if you are facing any problem or you find any error in the above program and also subscribe to our newsletter for more updates.


Read more c/c++ Programs published by snapcout:-

Program to remove spaces from given string
Program to sort a string in alphabetic order
Program to swap two strings
Program to convert string into lower case without using strlwr()
Program to convert string into lowercase using strlwr()


About article

Written By:- Admin

Published on:- 01/09/2018

Published By:- Snapcout


Don't forget to share the article if you like it:-