Find and replace string

Ok so im making a program that finds and replaces a word in a string of words. the thing is the program has to know where the word is and how long it is without it being hard coded int the program. My program attempts to get the word to be replaced from the user, then get the length of the word and replace it but im not sure what im 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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string enterStr;
    string findStr;
    int length = 0;
    int position = 0;

    cout << "enter a long string" << endl;
    getline(cin, enterStr);

    cout << "\n";
    cout << enterStr << "\n" << endl;

    cout << "What word do you want to find?" << endl;
    getline(cin, findStr);
    enterStr.find(findStr);
    length = findStr.length();
    position = findStr.at(length);
    enterStr.replace(position, length, "[replaced]");

    cout << enterStr << endl;
}
Last edited on
Bump please help.
Does anyone know? i need to be able to replace the string without hard coding it.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string enterStr;
    string findStr;
    string replace;
    int length = 0;
    int position = 0;

    cout << "enter a long string" << endl;
    getline(cin, enterStr);

    cout << "What word do you want to replace" << endl;
    getline(cin, findStr);
    length = findStr.length();
    position = findStr.find(findStr);
    findStr.replace(length, position, findStr);

    cout << enterStr << endl;


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	string str;
	cout<<"Enter the line: "<<endl;
	getline(cin,str);

	string str2 ;
	cout<<"Which word you want to find from the above line: ";
	cin>>str2;
	size_t found;


	found=str.find(str2);	  
	str.replace(str.find(str2),str2.length(),"[repalced]");
	cout << str << endl;
	

	system("pause");
	return 0;
}
Ok thanks, that works perfectly, now i want to replace [replaced] with a word that the user enters, i tried it but it doesnt work:

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

int main ()
{
	string str;
	cout<<"Enter the line: "<<endl;
	getline(cin,str);

	string str2 ;
	cout<<"Which word do you want to find from the above line: ";
	cin >> str2;
	size_t found;

	string str3;
	cout << "what do you want to replace it with?" << endl;
        getline(cin, str3);
        cin.get();

	found = str.find(str2);
	str.replace(str.find(str2), str2.length(), str3);
	cout << str << endl;


	cin.get();
	return 0;
}
Last edited on
bump, the block under str3 is what im having trouble with.
Last edited on
use cin.ignore(); instead of cin.get();


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>
using namespace std;

int main ()
{
	string str;
	cout<<"Enter the line: "<<endl;
	getline(cin,str);

	string str2 ;
	cout<<"Which word you want to find from the above line: ";
	cin>>str2;
	size_t found;


	found=str.find(str2);	
	cout<<"Enter a word: ";
	string str3;
	cin.ignore();
	getline(cin,str3);
	
	cout<<"After replacing with the given word: "<<endl;
	str.replace(str.find(str2),str2.length(),str3);
	cout << str << endl;
	

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.