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

In C++, Write a program that prompts the user to enter a string such as “Hello W

ID: 3823598 • Letter: I

Question

In C++, Write a program that prompts the user to enter a string such as “Hello World, I live at 678 San Diego”. Read the string and display the whole string first, then use string functions to display the following:

The length of the string

The character at index 8.

Turn the character at index 9 to upper case then display string again.

Check if the character at index 24 is a digit?

Display the ASCII code (integer value) for the character at index 10

Change the character ‘7’ to ‘9’ and display string again.

Explanation / Answer

Please find the required program and output below: Please find the comments against each line for the description:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
  
string str;
cout << "Enter the string:";
getline (cin, str); //read the input string
cout << "The length of the string : " << str.length() << endl; //print the length of the string
cout << "The character at index 8 : " << str[8] << endl; //printing the character at index 8
str[9] = toupper(str[9]); //converting char at 9 to uppercase
cout << "After converting char at 9 to upper : " << str << endl;
cout << "Check if the character at index 24 is a digit? " << isdigit(str[24]) << endl; //checking is digit
cout << " The ASCII code (integer value) for the character at index 10 : " << (int)str[10] << endl; //printing assii value
str[7] = str[9];
cout << "After changing the character ‘7’ to ‘9’ : " << str << endl;
}

-----------------------------------------------

OUTPUT: