strings repalcement

hi, i am new in programming , can any one tell me how to repalce the string with the other, i have made a program which takes different alphabets as input and display them as different words....but i am unable to print at the end only those alphabets which i have inserted and then only those words for which i have entered the alphabet (in a collective /combined manner)
can anyone help me out..?

output:(which i want)
enter msg :a
your msg is hello
u want to exit: n

enter msg :e
your msg is interesting
u want to exit: y

message you have typed : ae
message computer have generated : hellointeresting



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>
#include <stdio.h>
using namespace std;

int main()
{
        string s1;
	string s2;
        string s3;
	string s4;
	string s5;
	string s6;
	string s7;
	string strall;
	string msg;
	char ch;

	s1 = "a";
	s2="hello";
	s3="world";
	s4="this";
	s5="is";
	s6="interesting";
	s7="one";
    
	do{	
	cout << "enter msg : " << endl;
	cin>> s1; 

	if(s1=="a")
 {
	string s1 (s2);
    cout << "your msg is: " << s2 << endl;
 }

	else if(s1=="b")
 {
	string s1 (s3);
    cout << "your msg is: " << s3 << endl;
 }

	else if(s1=="c")
 {
	string s1 (s4);
    cout << "your msg is: " << s4 << endl;
 }

	else if(s1=="d")
 {
	string s1 (s5);
    cout << "encryptrd form is: " << s5 << endl;
 }

	else if(s1=="e")
 {
	string s1 (s6);
    cout << "your msg is: " << s6 << endl;
 }

		else if(s1=="f")
 {
	string s1 (s7);
    cout << "your msg is: " << s7 << endl;
 }
	else
 { 
    cout<<"exit"<<endl;
 }

cout<<"u want to exit"<<endl;
cin>>ch;
	
	
	
	}while(ch=='n');
	cout<<"message you typed is "<<msg<<endl;
	strall.append(s2);// not quite sure about this thing
	strall.append(s3);
	
	cout<<"the message computer gave is "<<strall<<endl;
		

system("pause");

}
Last edited on
This is the normal way of doing find/replace.
http://cplusplus.com/reference/string/string/find/
http://cplusplus.com/reference/string/string/replace/

1
2
3
4
5
6
string FindWord = "California";
string ReplWord = "Yorba";

string str = "Welcome to the Hotel California";

cout << str.replace(str.find(FindWord), FindWord.size(), ReplWord);
Welcome to the Hotel Yorba



That being said, I don't think that is what you are looking for. What you want is to store the new string. You can use a vector for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vector<string> inputs;
vector<string> outputs;
//............................................
// While loop and s1, s2, ... definitions up here somewhere
if(s1=="a")
{
    cout << "your msg is: " << s2 << endl;
    inputs.push_back(s1); // Store our input
    outputs.push_back(s2); // Store our replacement
}
else if
// The rest of your replacements here
//..............................................
vector<string>::iterator it;

cout << "message you typed is: ";
for (it = inputs.begin(); it < inputs.end(); ++it)
    cout << *it;

cout << "the message computer gave is ";
for (it = outputs.begin(); it < outputs.end(); ++it)
    cout << *it;
Last edited on
thanks alot it is working except for the

1
2
3
cout << "message you typed is: ";
for (it = inputs.begin(); it < inputs.end(); ++it)
    cout << *it;


it is not giving me the input (alphabets) which i have entered.
Topic archived. No new replies allowed.