Reverse a string using the pointer in C++


This article reverse a string using the pointer in C++ coding, First, it calculates the length of a pointer to string then run a for loop in decrement order to display the reverse string as follows;

Example

 Live Demo

#include <string.h>
#include <iostream>
using namespace std;
int main(){
   char *str="ajaykumar";
   cout<<"original string::"<<str;
   cout<<endl<<"String after reverse::";
   for(int i=(strlen(str)-1);i>=0;i--){
      cout<<str[i];
   }
   return 0;
}

This above program prints the to be supplied string “ajaykumar” in reverse order as follows.

Output

Original string::ajaykumar
String after reverse::ramukyaja

Updated on: 23-Dec-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements