122k views
4 votes
This program should declare a string of at most six characters. It should ask you to type in the string, and then print out the string and its third character on separate lines. It should then ask for a string again and print the string and its third character. It should repeatedly ask for a string, and print the output, until you type in a string starting with 's'. Then the program should end. When you are running the program try out these things: (a) What if you type in a string with more than six characters? (b) What if your string has only two characters? (c) What if it has only one character? 5

Use C++ language ​

1 Answer

0 votes

Answer:

Step-by-step explanation:

code:

"""
#include <iostream>

#include <string>

int main() {

std::string input;

do {

std::cout << "Enter a string of at most six characters: ";

std::cin >> input;

std::cout << "String: " << input << std::endl;

if (input.length() >= 2) {

std::cout << "Second character: " << input[1] << std::endl;

} else {

std::cout << "The string doesn't have a second character." << std::endl;

}

if (input.length() >= 3) {

std::cout << "Third character: " << input[2] << std::endl;

} else {

std::cout << "The string doesn't have a third character." << std::endl;

}

} while (input[0] != 's');

return 0;

}

"""

(A): If you type in a string with more than six characters, the program will only consider the first six characters of the input string. For example, if you enter "abcdefgh", it will only process "abcdef" and ignore the rest.

(B): If your string you have entered has only two characters, the program will still print the string and indicate that there is no third character.

(C): If your string has only one character, the program will also print the string and indicate that there is no second and third character.

User Liem
by
8.5k points

No related questions found