Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* 2170_2_2.cc Problem description: Write a program that (1) reads a string, (2)

ID: 3549630 • Letter: #

Question

/*  2170_2_2.cc
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) copy the string to modified and upperStr, (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 strings. (this step is done already)
    
    cout << "The original string is: " << original << endl
         << "After replacing C++ with Java: " << modified << endl
         << "Converting to upper case: " << upperStr << endl;
         
    return 0;
}
     

Explanation / Answer

#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);

int startIndex = 0;

int endIndex = 0;

while(original.find("C++",startIndex)!=string::npos)

{

endIndex = original.find("C++",startIndex);

modified+= original.substr(startIndex,endIndex-startIndex);

modified = modified + "Java";

startIndex = endIndex+3;

}

modified+=original.substr(startIndex,original.length()-startIndex);

//converting to uppercase

int i=0;

upperStr = modified;

while(i<upperStr.length())

{

if(upperStr[i]>=97 && upperStr[i]<=122)

upperStr[i]-=32;

i++;

}


cout << "The original string is: " << original <<endl

<< "After replacing C++ with Java: " << modified << endl

<< "Converting to upper case: " << upperStr << endl;


return 0;

}