Reversing a string input

Hello,

I am trying to have an input statement inside a loop
to input a string and when it sees a period in the end it breaks
and display the string in reversed order.

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
  

#include<iostream>
#include<string>

using namespace std;

class Stack
{
public:
	static const unsigned MAX_SIZE = 8;
	Stack();
	unsigned size()const;
	void push(const string & item);
	string pop();
	string top();
private:
	string data[MAX_SIZE];
	unsigned elments;

};

bool die(const string & msg);

int main()
{
	Stack s;
	string x;
	//getline(cin, x);
	//s.push(x);
	//cout << s.pop() << endl;
	
	while (x != ".")
	{
		
		cin >> x;
		s.push(x);
		if (s.top() == ".")
		break;
	}
	cout << reverse(x,x-1) << endl;
	/*
		//for (; x != ".";)
		//{
			//cin >> x;
			//s.push(x);
			//break;
		//}
	//cout << s.pop() << endl;
	*/
}//main

Stack::Stack()
{
	elments = 0;
}

unsigned Stack::size() const
{
	return elments;
}

void Stack::push(const string & item)
{
	if (elments == MAX_SIZE) die("Overflow");
	data[elments++] = item;
}

string Stack::pop()
{
	if (elments == 0) die("Underflow");
	return data[--elments];
}

string Stack::top()
{
	if (elments == 0) die("Underflow");
	return data[elments - 1];
}

bool die(const string & msg)
{
	cerr << "Fatal error: " << msg << endl;
	exit(EXIT_FAILURE);
}
I am trying to

... and?

Can you describe the problem/question that you have?
this is my question " input statement inside a loop
to input a string and when it sees a period in the end it breaks
and display the string in reversed order. "
That looks more like the objective than a question.

You already have some code. Does it have problems?

What happens on line 41?

How will your program react to these three inputs:
Hello world .

Hello world.

Hello world
I am having difficulty in writing a loop that can input bunch strings and when it sees a period it exits the loop and display the whole strings backwards. Is the question clear now?

the examples you suggested won't work since my code is not correct!
Last edited on
1
2
3
while ( cin >> x && /*something*/ ) {
  s.push( x );
}

http://www.cplusplus.com/reference/string/string/back/
not quite , I still can't figure it out is my cout statement right ?

1
2
3
4
while (cin >> x && x != ".") {
		s.push(x);
	}
	cout << s.pop() << endl;


Printing in reverse is an entirely separate task. Think, why you do use a stack for storing the words.


I now see that my code cannot properly do the necessary logic. What I had in mind would have discarded the last word. Again:
1
2
3
4
5
6
7
while ( cin >> word ) {
  // there are now four possibilities:
  // * word is empty -- skip it
  // * word is just a word -- store it
  // * word ends with a dot -- store and break
  // * word is a dot -- (store? and) break
}

I did give you the link to std::string::back() for a purpose.
Topic archived. No new replies allowed.