Colour Theme Font Size Options
 
 
 
Exercise 4

String Exercise 2: Something Completely Different!

Now it's your turn to write a program using the .length(), .find() and .substr() string functions. You should understand String Exercise 1 before you try this exercise.

Use the file ex4_stringB.cpp in the replit team project to write a program. Follow the steps below:

  • Declare a string variable: originalString.
  • Assign "And now for something completely different." to originalString.
  • Display the content of originalString.
  • Display the length of originalString.
  • Declare a string variable, srch to store the word "completely ".
  • Use srch to find the position of "completely " in the originalString.
  • Display the position of completely.
  • Declare a string variable: newString.
  • Use the substr() function to get this sub-string of originalString: "And now for something ".
    then assign it to newString.
  • Output newString.
  • Get a sub-string of originalString with value "different.".
    Add this substring to newString.
  • Display newString. The output should look like this:
    And now for something different.
  • Compile and run this C++ program.

When you are done:

  • Take a screenshot of your IDE and console window after running the program.
  • For this exercise, you will have: ex4_stringB.cpp.
  • If you have completed everything, congratulations! Please upload files to URCourses.

IMPORTANT: You should use the position and length of srch to complete the sub-string instructions in your program.