Can't copy and reverse the string!!!

Dear All,

Following are the codes for copy and reverse thhe sentence by using strings.
But as i run the codes it only except character and cursor stays at output point without and character.

#include<iostream>
using namespace std;
class Swap_Rev_Str
{
private:
int pos,j,i;
string source,dest;
public:

void Enter()
{
cout<<"Enter Sentence :";
getline(cin,source);
for(i=0;source[i]!='\0';i++)
{
pos+=1;
}
}
void Reverse()
{
for(j=0;j<pos;j++)
{
--pos;
dest[j]=source[pos];

}
dest[j]='\0';
cout<<"\nCopied and Reversed Sentence is :"<<dest;
}
};
int main()
{
Swap_Rev_Str S1;
S1.Enter();
S1.Reverse();
getchar ();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
return 0;
}
You can try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>

using namespace std;

class StringReverse
{
public:
	void GetString();
	void ReverseString();
	void PrintString();
private:
	string szSource;
	string szDest;
};

void StringReverse::GetString()
{
	cin>>szSource;
}

void StringReverse::ReverseString()
{
	char szDestArray[256];
	int iLength = 0,iPos = 0;
	while(szSource[++iLength]!='\0');
	szDestArray[iLength]='\0';
	for(int iCount=iLength-1; iCount>=0;iCount--)
	{
		szDestArray[iPos] = szSource[iCount];
		iPos++;
	}
	szDest = szDestArray;
}

void StringReverse::PrintString()
{
	cout<<szDest<<endl;
}

int main()
{
	StringReverse sObj;
	sObj.GetString();
	sObj.ReverseString();
	sObj.PrintString();

	return 0;
}
Done, thanks elpis

but is i enter the sentence with space, the reversse of that sting shows me the characters which i inserted before space.

What could be the solution.

It is a string than it should except the spaces as well, right?

Thanbks again.
Please use getline(cin,szSource); instead of cin>>szSource;

Refer http://www.cplusplus.com/doc/tutorial/basic_io.html
Topic archived. No new replies allowed.