I need help to finish this program: Problem description: Write a program that (1
ID: 3619948 • Letter: I
Question
I need help to finish this program:Problem description: Write a program that (1) reads a string,
(2) replaces every occurrence of the substring "C++" by a string "Java",
(3) converts every alphabet in the string to upper case, and
(4) output the final string.
For example, if the input is:
I love C++! C++ is fun.
after replacing "C++" by "Java", it becomes
I love Java! Java is fun.
after converting each alphabet to upper case, it becomes
I LOVE JAVA! JAVA IS FUN.
You are only allowed to call the following string functions:
length: get the number of chars in the string
substr: get a specified substring
find: find the position of a substring
[]: accessing a char at a specific position in the string.
In other words, a string can be viewed and accessed as an array of chars.
str1 = str2; copy the string str2 to the string str1
str = str1 + str2; concatenate str1 and str2, and save it to str.
*/
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string original; //contains input string
string modified; //the string obtained by replacing "C++" with "Java"
string upperStr; //convert chars in the string "modified" to upper case
int pos; //the starting position of the substr "C++"
getline(cin, original);
Write a code segment that
(1) reads a string,
(2) replaces every occurrence of the substring "C++" by a string "Java",
(3) converts every alphabet in the string to upper case,
(4) output the final string.
Write a code segment that (1) reads a string,
(2) replaces every occurrence of the substring "C++" by a string "Java",
(3) converts every alphabet in the string to upper case, and
(4) output the final string.
cout << "The original string is: " << original << endl
<< "After replacing C++ with Java: " << modified << endl
<< "Converting to upper case: " << upperStr << endl;
return 0;
}
Explanation / Answer
please rate - thanks this version allows you to input what to replace and what to repl;ace it with. much more general, then other answer #include<iostream.h>#include <string.h>
using namespace std;
string replaceString(string ,string,string);
int main()
{string input, lookfor, replace;
cout<<"Enter a string: ";
getline(cin,input);
cout<<"What do you want to replace? ";
getline(cin,lookfor);
cout<<"What do you want to replace it with? ";
getline(cin,replace);
input=replaceString(input,lookfor,replace);
cout<<"The new string is: "<<input<<endl;
system("pause");
return 0;
}
string replaceString(string in ,string change,string to)
{string newstring="",temp;
int len=change.length();
int n=0;
int pos=in.find(change,n);
while (pos >=0)
{
temp=in.substr(n,pos-n);
newstring=newstring+temp;
newstring=newstring+to;
n=pos+len;
pos=in.find(change,n);
}
newstring=newstring+in.substr(n,in.length()-n);
for(int i=0;i<newstring.length();i++)
newstring[i]=toupper(newstring[i]);
return newstring;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.