In: Computer Science
2) You've been hired by Superfluous Stats to write a C++ console application that analyzes a string. Prompt for and read from the user a string that may contain spaces. Then prompt for and read from the user a single character. Print the following stats about the string and character inputs. Use formatted output manipulators (setw, left/right) to print the following rows about the string:
● The string value.
● The string length.
● The spot, if any, where the character is found within the string.
And the following rows about the character:
● The character value.
● Whether the character is an alphabetic character.
● Whether the character is a numeric character.
● Whether the character is a punctuation character.
And columns:
● A left-justified label.
● A right-justified value.
Define constants for the column widths. The output should look like this:
Welcome to Superfluous Stats
----------------------------
Enter a string: Detroit Michigan
Enter a character: r
String s
Value: Detroit Michigan
Length: 16
Index: 3
Character c
Value: r
Is alpha? 2
Is digit? 0
Is punctuation? 0
End of Superfluous Stats
Note that you may get a value for true that is not 1. Do not use this sample input for the final run that is pasted below.
Solution:
#include<iostream>
#include<string>
#include<ctype.h>
#include<iomanip>
using namespace std;
int main()
{
char s[257];
char c;
int i;
cout<<"Welcome to Superfluous Stats\n";
cout<<"----------------------\n";
cout<<"Enter a string: ";
cin.getline(s,256);
cout<<"Enter a character: ";
cin>>c;
int n=string(s).length();
cout<<"String s\n";
cout<<"Value:"<<setw(30)<<s<<"\n";
cout<<"Length:"<<setw(25)<<n<<"\n";
i=0;
for(char x:s)
{
if(x==c)
break;
i++;
}
if(i==n)
i=-1;
cout<<"Index:"<<setw(25)<<i<<"\n";
cout<<"Character c\n";
cout<<"Value:"<<setw(25)<<c<<"\n";
cout<<"Is alpha?"<<setw(22)<<isalpha(c)<<"\n";
cout<<"Is digit?"<<setw(22)<<isdigit(c)<<"\n";
cout<<"Is punctutation?"<<setw(15)<<ispunct(c)<<"\n";
cout<<"End of Superfluous Stats";
return 0;
}
Output: