Program to reverse string using pointers

12 months ago Admin 0

Reverse string using pointers

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

reverse string using pointers

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

C Programming language

Program


#include<stdio.h>
#include<conio.h>
void main()
{
 char str[100],res[100];
 int i=0;
 char *strp=str;
 char *resp=res;
 clrscr();
 printf("Enter a string :- ");
 gets(str);

 while(*strp)
  {
   strp++;
   i++;
  }
 while(i>0)
  {
   strp--;
   *resp=*strp;
   resp++;
   --i;
  }
 *resp='\0';
 printf("Reverse of a string is:- %s",res);
 getch();
}

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

reverse string using pointers


C++ Programming language

Program


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 char str[100],res[100];
 int i=0;
 char *strp=str;
 char *resp=res;
 cout<<"Enter a string :- ";
 cin>>str;

 while(*strp)
  {
   strp++;
   i++;
  }
 while(i>0)
  {
   strp--;
   *resp=*strp;
   resp++;
   --i;
  }
 *resp='\0';
 cout<<"Reverse of a string is:- "<<res;
 getch();
}

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

reverse string using pointers


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 add two numbers using pointers
Program to swap two numbers using pointers
Program to remove spaces from given string
Program to sort a string in alphabetic order
Program to swap two strings


About article

Written By:- Admin

Published on:- 26/08/2018

Published By:- Snapcout


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