Substrings

I am learning sub-string find and replace. I have written a program that does other things besides that, however the part I need help with is to have a user enter a sentence which I store as a string, and then enter a sub-string which is to be part of the original string, and then replace it with another sub-string of their choice. I can't figure out how to determine the length of the sub-strings and also how to tell the program to look for all occurrences of the sub-string i the original string and replace them all. example: if the string is mississippi, and the user wants to replace iss with hello it should read mhellohelloippi. What am I doing wrong?
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

void str_to_upper(string &s);
void reverse(string &s);
 
void reverse(string &s);
void replace(string &s, char x, char y);
void sreplace(string &s, string sub_replaced, string sub_with);

int main()
{
        string s;
        cout << "Welcome to editor. Please enter your first sentence or *quit* to end." << endl;
        getline(cin, s);

        while(s != "quit"){
                cout << "Please choose an editing operation:"
                        << "\nH to replace all characters x with characters y"
                        << "\nR to reverse the sentence"
                        << "\nU to change the sentence to upper case."
                        << "\nS to replace substring s1 with s2." << endl;
                char inp;
                cin >> inp;
                cin.ignore();
                int counter = 1;
                while(inp != 'H' && inp != 'R' && inp != 'U' && inp != 'S'){
                        if(counter >= 3)
                        {
                                return 0;
                        }//if
                        cout << "Please use H, R, U, or S to specify the editing choice. Please try again." << endl;
                        counter++;
                        cin >> inp;
                        cin.ignore();
                }//while
                switch(inp)
                {
                    case 'H':
                    {
                        cout << "Please enter the first character:" << endl ;
                        char x, y;
                        cin >> x;
                        cout << "Please enter the second character:" << endl;
                        cin >> y;
                        cin.ignore();
                        replace(s, x, y);
                        cout << s << endl;
                    }
                      
			  break;

                    case 'R':
                    {
                        //print the string in the reverse order
                        reverse(s);
                        cout << s << endl;
                    }
                        break;

                    case 'U':
                    {
                        //change string to upper case and print it
                        str_to_upper(s);
                        cout << s << endl;
                    }
                        break;

                    case 'S':
                    {
                        cout << "PLease enter the first substring:" <<endl;
                        string s1 = sub_replaced;
                        string s2 = sub_with;
                        string a_s1;
                        string b_s1;
                        cin >> sub_replaced;
                        cout << "Please enter the second substring:" <<endl;
                        cin >> sub_with;
                        cin.ignore();
                        find(string s, string s1);
                        sreplace(string s, sub_replaced, sub_with);
                        cout << s << endl;
                    }
                        break;
                }//End of Switch

                cout << "Please enter next sentence or *quit* to end." << endl;
                getline(cin, s);
        }//while

        return 0;
}

void str_to_upper(string &s){
        for(int i = 0; i < s.length(); i++){
                s[i] = toupper(s[i]);
        }//for
}//str_to_upper()

void replace(string &s, char x, char y){
        for(int i = 0; i < s.length(); i++){
                if(s[i] == x)
                        s[i] = y;
        }//for
}//replace()

void reverse(string &s){
        for( int i=0, j=s.length()-1; i < j; i++, j--){
                char temp = s[i];
                s[i] = s[j];
                s[j] = temp;
        }
}//reverse()

void find(string s, string s1){
        for(int i = 0; i <= s.length() - s1.length(); i++)
        {
                string s1 = s.substr(i, s1.length());
                if(s1 == s2)
                {
                        b_s1 = s.length() - a_s1.length() - s1.length();
                        a_s1 = s.length() - b_s1.length() - s1.length();
                        s = b_s1 + s2 + a_s1;
                        i = i + s2.length()-1;
                }

        }
}//find()

void sreplace(string &s, string &sub_replaced, string &sub_with)
{
        for(int i = 0; i <= s.length(); i++)
        {
                s1 = s2;
}
closed account (o3hC5Di1)
Hi there,

First off, I'm not sure if your aware, but some of these functions are actually already implemented in the string class:

http://www.cplusplus.com/reference/string/string/

What I would do is to loop string.find(), which returns you the position of the first substring match, then use string.replace() to do the actual replacement.

I'm just giving you some hints and telling you where to look, please do let us know if you need any further help.

All the best,
NwN
Topic archived. No new replies allowed.