Friday, June 5, 2015

C++ programs to reverse a given string without using inbuilt functions

/*-------------------------------------------------------------------------------------------------------------------------------
Author: Sujith
Program: Write C++ programs to reverse a given string without using inbuilt functions
Prog Name: 1strrev.cpp
Date: 21-04-2015
---------------------------------------------------------------------------------------------------------------------------------*/
#include<iostream>
using namespace std;
void fnStrRev(char s1[30],char s2[30]);
main()
{
    char s1[30],s2[30];
     system("clear");
     cout<<"Enter the string to be reversed\n";
     cin>>s1;
     fnStrRev(s1,s2);
     cout<<"The reversed string is : "<<s2<<"\n";
}
void fnStrRev(char s1[30],char s2[30])
{
     int i,j,n=0;
     for(i=0;s1[i]!='\0';i++);
        for(j=i-1;j>=0;j--){
            s2[n]=s1[j];
            n++;
        }
s2[n]='\0';
}

Output:
[141740@localhost ~]$ g++ strrev.cpp
[141740@localhost ~]$ ./a.out
Enter the string to be reversed
sujith
The reversed string is : htijus
[141740@localhost ~]$

1 comment:

  1. C++ Program to Reverse a Strings

    Reverse a String means reverse the position of all character of String. You can use swapping concept to reverse any string in c++.

    ReplyDelete