Original Post
Hi guys, I have recently been mucking about with how to try and reverse a string entered by a user. Anyway, as you may have guessed from the fact i am posting here i am not having much luck. This is what i have so far Now everything works up until the 2nd for loop where i try and populate the reversed string. I have just got it working using a char array which i guess i could then easily turn back into a straing but is there any way that i could do this without having to use the char array as an intermediate storage? Cheers Neil
[source lang = "cpp"]
#include <iostream>
#include <string>
using namespace std;
int length;
int tempLength;
string sentence;
string revString;
int main()
{
cout<<"Enter a sentence"<<endl;
getline(cin,sentence);
length = sentence.length();
cout<<length<<endl;
tempLength = length;
cout<<tempLength<<endl;;
cout<<sentence[length-4];
for(int i=0;i<tempLength;i++)
{
cout<<sentence[length]<<endl;
length--;
}
for (int j=0;j<tempLength;j++)
{
cout<<revString[j]<<endl;
}
// cout<<length<<endl;
cout<<endl;
cout<<sentence<<endl;
system("PAUSE");
return 0;
}