Complete the following C++ questions: 4.Prompt the user for a line of text. Outp
ID: 3835017 • Letter: C
Question
Complete the following C++ questions:
4.Prompt the user for a line of text. Output that line in all capitals. Leave non-alphabetic characters untouched.
required output:
Test Case 2
Test Case 3
Test Case 4
5.Prompt the user for two strings, s1 and s2. Determine if s1 and s2 are equal. If they're not equal, output them in alphabetical order.
Required output:
Test Case 2
Test Case 3
Test Case 4
Test Case 5
Test Case 6
Test Case 7
Test Case 8
Test Case 9
6.Prompt the user for his/her name. Verify that the name only contains alphabetic characters.
Required output:
Test Case 1
Test Case 2
Test Case 3
Test Case 4
Test Case 5
Test Case 6
Test Case 7
Test Case 8
7.Prompt the user for a social security number. Determine if the SSN is in the correct format.
required output:
8.Determine if a word entered by the user is spelled correctly. A word is considered correct if it's found in dict.txt (see required output to get file).
Required output:
dict.txt
this is a test.
Explanation / Answer
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
// 4
cout << "Enter a line of text" <<endl;
cin.ignore();
getline(cin, str);
for(int i = 0; i < str.length(); i++)
{
if(isalpha(str[i]))
{
str[i] = toupper(str[i]);
}
cout << str[i];
}
cout << endl;
// 5
string str2;
cout << "== String Compare ==" <<endl;
cout << "Enter a word" <<endl;
cin >> str;
cout << "Enter another word" <<endl;
cin >> str2;
if(str == str2)
{
cout << "Words are exactly the same." << endl;
}
else if(str > str2)
{
cout << """ << str << "" comes before "" << str2 << ""." << endl;
}
else
{
cout << """ << str << "" comes after "" << str2 << ""." << endl;
}
// 6
cout << "What's your first name?" << endl;
cin >> str;
bool isValid = true;
for(int i = 0; i < str.length(); i++)
{
if(!isalpha(str[i]))
{
isValid = false;
break;
}
}
if(isValid)
{
cout << "Valid name." << endl;
}
else
{
cout << "Invalid name." << endl;
}
// 7
cout << "Enter your SSN in this format: XXX-XX-XXXX"<<endl;
cin >> str;
if(str.length() != 11)
{
cout << "Problem: You must type exactly 11 characters." << endl;
}
else if(str[3] != '-' || str[6] != '-')
{
cout << "Problem: The dashes are missing or are in the wrong spot" << endl;
}
else
{
bool isValid = true;
for(int i = 0; i < str.length(); i++)
{
if(i == 3 || i == 6)
{
continue;
}
else if(!isdigit(str[i]))
{
isValid = false;
cout << "Problem: Only digits are allowed in a SSN" << endl;
break;
}
}
if(isValid)
{
cout << "That is valid."<< endl;
}
}
return 0;
}