Reverse a String ??

#include <iostream>
using namespace std ;
//Instead of using length functon you can use a inbuilt function called 'strlen' but if you want to
//use it then you must include <cstring> library. Program will become lot easier.
int length(char input[]) {
int count{0};
for (int i = 0; input[i] != '\0'; ++i) {
count++;
}
return count;
}

void reverse(char input[]){
int start{0} ;
int end{length(input) - 1} ;
while (start < end){
swap(input[start],input[end]);
start ++ ;
end -- ;
}
}

int main(){
char name[100] ;
cout << "Enter string : " ;
cin.getline(name,100);
reverse(name) ;
cout << "Reversed string is " << name ;


}

Comments

Popular posts from this blog

CodeChef::CSUB

How Recursion Works?

CodeChef::TREE2