About Me

My photo
Mostly software programming related blogs.

Friday, October 26, 2012

Reverse a string



C/C++ solution:

using namespace std;

#include<iostream>
#include<string.h>

string reverse(string input);

int main() {
        string input;
        cout << "Enter the string : " ;
        getline(cin, input);
        string output = reverse(input);
        cout << "Revered string :" << output << "\n";
}

string reverse(string input) {
        int len = input.length();
        int i = 0;
        char tmp;
        while(i < len/2) {
                tmp = input[i];
                input[i] = input[len -i -1];
                input[len - i - 1] = tmp;
                i++;
        }  
        return input;
}

No comments: