Problems with exercise using putback()

Hi, im trying to create program that gets text input and than transforms don't into do not. Later i will have to remove (' , . ? -) as well from text.

So i tryed to look for d letter and than check what characters will come after that. If i find that they are 'd' 'o' 'n' '\' 't' i tryed to put back "do not" into the stringstream. If not than just put back all the letters i took.

PS. This probably is ultra bad way for this problem, so i would be happy to hear how to solve this in a smrter way

In this code if i enter
don't
as input im getting output
d

In this code if i enter
don't
and using a single whitespace after it as input im getting output
do not


Can anyone explain whats happening here?

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
void text_without_dont(){

	cout << "Enter text\n";
	string text;
	getline(cin, text);
	stringstream ss{ text };

	char ch;
	while (ss.get(ch)){
		if (ch == 'd'){
			string temp;
			int characters = 4;
			while (ss.get(ch) && characters > 0){
				temp += ch;
				characters--;
			}
			if (temp == "on't") {
				ss.putback('t');
				ss.putback('o');
				ss.putback('n');
				ss.putback(' ');
				ss.putback('o');
				cout << "d";
				continue;
			}
			else{
				for (int i = temp.size() - 1; i >= 0; i--){
					ss.putback(temp[i]);
				}
				cout << "d";
				continue;
			}
		}
		//cout the line without don't in it
		cout << ch;
	}

}
Last edited on
I don't think putback or stringstreams are the right choice for this problem. I would just use regular string functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <algorithm>


int main()
{
	std::string sentence = "I don't like you I don't like you";

	auto pos = sentence.find("don't");
	
	while (pos != std::string::npos)
	{
		sentence.replace(pos, 5, "do not");
		pos = sentence.find("don't");
	}
	
	std::cout << sentence << std::endl;
	std::cin.ignore();
	return 0;
}
Last edited on
Cool that's exactly what i wanted - better solution for my problem. Thanks a lot. I will still leave this unanswered coz i want to know why my version didn't work
you should do that as Yanson showed.
you are trying in a difficult way. there are more errors in your code than you showed.
but you may want to compare with correct one:
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;


int main() {

	
while(1){ /// infinite test

	cout << "Enter text\n";
	string text;
	getline(cin, text);
	stringstream ss(text);

	char ch;
	while (ss.get(ch)){
		if (ch == 'd'){			
			string temp;
			int characters = 4;
			while (characters > 0 && ss.get(ch)){				
				temp += ch;
				characters--;
			}
			if (temp == "on't") {	
				cout << "do not";
				continue;
			}
			else{
				cout << "d" << temp;
				continue;
			}
		}
		
		cout << ch;
	}
	cout << "\n\n";
	
}///
return 0;
}

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

int main() {

while(1){ /// infinite test

	cout << "Enter text\n";
	string text;
	getline(cin, text);
	stringstream ss(text);

	char ch;
	while (ss.get(ch)){
		cout << ch;
		if (ch == 'd'){			
			string temp;
			int characters = 4;
			while (characters--) temp += ss.get();			
			if (temp == "on't") cout << "o not";		
			else cout << temp;			
		}		
	}
	cout << "\n\n";
	
}///
return 0;
}
Thanks you guys a ton. Really cool to learn from the best :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main() {

	string s= "please don't try this at home!";
	
	for(int i=0; i<s.size(); i++){
		cout << s[i];
		if(s[i]=='d' && s[i+1]=='o' && s[i+2]=='n' && s[i+3]=='\'' && s[i+4]=='t' ){
			cout << "o not";
			i+=4;
		}
	}
}
Topic archived. No new replies allowed.