Hi guys,
Need some basic help here. Basically I have to write a program that will search a string and replace all occurrences of "C++" in that string with "Java". The program will then have to convert the updated string to upper case letters. I've got it working great for strings that have more than one instance of "C++" but if the string only has it in there once it messes up. Can anyone give me some guidance?
Here is my code:
Code:
#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);
// Anything above this cannot be edited because it was all ready in the code given to us
string temp;
pos = original.find("C++");
modified = original.substr(0, pos) + "Java";
temp = original.substr(pos + 3);
pos = temp.find("C++");
modified += temp.substr(0, pos) + "Java" + temp.substr(pos + 3);
upperStr = modified;
for (int i = 0; i < upperStr.length(); i++)
{
upperStr[i] = toupper(upperStr[i]);
}
// End of editable code
cout << "The original string is: \n\t" << original << endl
<< "After replacing C++ with Java: \n\t" << modified << endl
<< "Converting to upper case: \n\t" << upperStr << endl;
return 0;
}
Any help would be greatly appreciated!