Letter reverser

Ok, so I have to create a program that will take in a string, then reverse each individual letter. So "Hello there john" would be output as "olleH ereht nhoj".

I currently have the reverse function working, but it reverses the entire string, not just the individual words. So far, my code is:

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
#include <iostream>
#include <string>
using namespace std;


string reverse(const string& s);

int main()
{ 
	//Variable declarations
	string input, reversed = "";
	
	//Gets the string from the user
	cout << "Enter your line of string: ";

	do{
		cin >> input;
		reversed = reversed + reverse(input);
	}while(input != '\n'); 

	cout << "Your reversed string is: " << reversed << endl;

	return 0;
}

string reverse(const string& s)
{
	int start = 0;
	int end = s.length();
	string temp(s);
	
	while(start < end)
	{
		end--;
		swap(temp[start], temp[end]);
		start++;
	}
	return temp;
}


The do-while loop is where I'm having difficulty. I know that it is incorrect, but that is the last thing I had tried before posting. I also know using the >> operator will read in from the string until a space is hit, but I don't know how to make it iterate until the end of the string, which is what I'm trying to do.

Any tips?

Thanks
Hi, Here is the solution, I take the input which is a string and break it up in chunks using the space as the delimiter and store it in vector and pass it to the reverse function and woah, It works ! :)
Cheers, Toshitaka
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string reverse(const string& s);
void StringExplode(string str, string separator, vector<string>* results);

int main()
{

	//Variable declarations
	string input, reversed = " ";
    vector<string> R;

	//Gets the string from the user
	cout << "Enter your line of string: ";

	//do{
		getline (cin, input);
		StringExplode(input," ", &R);

		for(int i=0;i<R.size();i++)
		reversed = reversed +" "+reverse(R[i]);
	//}while(input != "\n");

	cout << "Your reversed#include <iostream>
#include <string>
#include <vector>
using namespace std;

string reverse(const string& s);
void StringExplode(string str, string separator, vector<string>* results);

int main()
{

	//Variable declarations
	string input, reversed = " ";
    vector<string> R;

	//Gets the string from the user
	cout << "Enter your line of string: ";

	//do{
		getline (cin, input);
		StringExplode(input," ", &R);

		for(int i=0;i<R.size();i++)
		reversed = reversed +" "+reverse(R[i]);
	//}while(input != "\n");

	cout << "Your reversed string is: " << reversed << endl;

	return 0;
}

string reverse(const string& s)
{
	int start = 0;
	int end = s.length();
	string temp(s);

	while(start < end)
	{
		end--;
		swap(temp[start], temp[end]);
		start++;
	}
	return temp;
}


void StringExplode(string str, string separator, vector<string>* results){
    int found;
    found = str.find_first_of(separator);
    while(found != string::npos){
        if(found > 0){
            results->push_back(str.substr(0,found));
        }
        str = str.substr(found+1);
        found = str.find_first_of(separator);
    }
    if(str.length() > 0){
        results->push_back(str);
    }
}

 
Thanks for the help, but we aren't allowed to use vectors. Haven't been taught about those in class yet. :/
Thanks for the help, but we aren't allowed to use vectors. Haven't been taught about those in class yet. :/
Learn them. Impress your prof.
I have not been able to do what you want using C++ strings. This is where i got to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int x=0,count=0;
    string s;
    cout<<"\n";
    s="Hello there John";
    cout<<s<<endl;
    x=s.length();
    char* p1=&s[0];//place pointers at both ends of the string
    char* p2=&s[x-1];
    
    while(x/2>count)//now swap the characters as pointers
                    //move towards the center of string
    {
     swap(*p1,*p2);
     p1++;p2--;
     count++;
    }
    cout<<s<<endl;
  
cout<<"press any key...";
cin.get();
return 0;
}  

/*
Hello there John
nhoJ ereht olleH
press any key...
*/
I think that C string manipulation is required but will be interested in what the experts come up with
Topic archived. No new replies allowed.