PLease write in C 1 Prompt the user to enter a string of the
PLease write in C.
(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)
Ex:
(2) Implement a PrintMenu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user\'s entered menu option. Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit. (3 pts)
Ex:
(3) Implement the GetNumOfNonWSCharacters() function. GetNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. Hint: Using fgets() to read input will cause your string to have a newline character at the end. The newline character should not be counted by GetNumOfNonWSCharacters(). Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)
Ex:
(4) Implement the GetNumOfWords() function. GetNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call GetNumOfWords() in the PrintMenu() function. (3 pts)
Ex:
(5) Implement the FixCapitalization() function. FixCapitalization() has a string parameter and updates the string by replacing lowercase letters at the beginning of sentences with uppercase letters. FixCapitalization() DOES NOT output the string. Call FixCapitalization() in the PrintMenu() function, and then output the edited string. (3 pts)
Ex:
(6) Implement the ReplaceExclamation() function. ReplaceExclamation() has a string parameter and updates the string by replacing each \'!\' character in the string with a \'.\' character. ReplaceExclamation() DOES NOT output the string. Call ReplaceExclamation() in the PrintMenu() function, and then output the edited string. (3 pts)
Ex.
(7) Implement the ShortenSpace() function. ShortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. ShortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. (3 pt)
Ex:
Here is some starting code
#include<stdio.h>
#include <string.h>
#include <ctype.h>
int GetNumOfNonWSCharacters(const char usrStr[]) {
}
int GetNumOfWords(const char usrStr[]) {
}
void FixCapitalization(char usrStr[]) {
}
void ReplaceExclamation(char usrStr[]) {
}
void ShortenSpace(char usrStr[]) {
}
char PrintMenu(char usrStr[]) {
char menuOp = \' \';
printf(\"MENU\ \");
printf(\"c - Number of non-whitespace characters\ \");
printf(\"w - Number of words\ \");
printf(\"f - Fix capitalization\ \");
printf(\"r - Replace all !\\\'s\ \");
printf(\"s - Shorten spaces\ \");
printf(\"q - Quit\ \ \");
while (menuOp != \'c\' && menuOp != \'w\' && menuOp != \'f\' &&
menuOp != \'r\' && menuOp != \'s\' && menuOp != \'o\' &&
menuOp != \'q\') {
printf(\"Choose an option:\ \");
scanf(\" %c\", &menuOp);
}
if (menuOp == \'c\') {
printf(\"Number of non-whitespace characters: %d\ \ \", GetNumOfNonWSCharacters(usrStr));
menuOp = \' \';
}
else if (menuOp == \'w\') {
printf(\"Number of words: %d\ \ \", GetNumOfWords(usrStr));
menuOp = \' \';
}
else if (menuOp == \'f\') {
FixCapitalization(usrStr);
printf(\"Edited text: %s\ \", usrStr);
menuOp = \' \';
}
else if (menuOp == \'r\') {
ReplaceExclamation(usrStr);
printf(\"Edited text: %s\ \", usrStr);
menuOp = \' \';
}
else if (menuOp == \'s\') {
ShortenSpace(usrStr);
printf(\"Edited text: %s\ \", usrStr);
menuOp = \' \';
}
return menuOp;
}
int main() {
char userString[256];
char menuChoice = \' \';
printf(\"Enter a sample text:\ \");
fgets(userString, 256, stdin);
printf(\"\ \");
printf(\"You entered: %s\ \", userString);
while (menuChoice != \'q\') {
menuChoice = PrintMenu(userString);
}
Solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>.
int GetNumOfNonWSCharacters(const char usrStr[]) {
int length;
int i;
int count = 0;
char c;
length=strlen(usrStr);
for (i = 0; i < length; i++) {
c=usrStr[i];
if ( c!=\' \' )
{
count++;
}
}
return count;
}
int GetNumOfWords(const char usrStr[]) {
int counted = 0; // result
// state:
const char* it = usrStr;
int inword = 0;
do switch(*it) {
case \'\\0\':
case \' \': case \'\\t\': case \'\ \': case \'\ \': // TODO others?
if (inword) { inword = 0; counted++; }
break;
default: inword = 1;
} while(*it++);
return counted;
}
void FixCapitalization(char usrStr[]) {
int length;
int i;
int count = 0;
char c;
length=strlen(usrStr);
for (i = 0; i < length; i++) {
c=usrStr[i];
if(i==0)
{
usrStr[i]=toupper(usrStr[i]);
}
if ( c==\'.\' )
{
if( usrStr[i+1]!=\' \')
{
usrStr[i+1]=toupper(usrStr[i+1]);
}
else
{
usrStr[i+2]=toupper(usrStr[i+2]);
}
}
}
printf(\"%s\",usrStr);
}
void ReplaceExclamation(char usrStr[]) {
int i;
for(i=0;usrStr[i]!=\'\\0\';i++)
{
if(usrStr[i]==\'!\')
{
usrStr[i]=\'.\';
}
}
printf(\"%s\",usrStr);
}
void ShortenSpace(char usrStr[]) {
int j,length=1,i;
for(i=0;usrStr[i]!=\'\\0\';i++)
{
if(usrStr[i]==\' \')
{
length=0;
for(j=i+1;usrStr[j]==\' \';j++)
{
length++;
}
if(length!=0)
{
for(j=i+1;usrStr[j]!=\'\\0\';j++)
{
usrStr[j]=usrStr[j+length];
}
}
}
}
printf(\"%s\",usrStr);
}
void PrintMenu(char userString[])
{
char choice;
do
{
printf(\"MENU\ \");
printf(\"c - Number of non-whitespace characters\ \");
printf(\"w - Number of words\ \");
printf(\"f - Fix capitalization\ \");
printf(\"r - Replace all !\\\'s\ \");
printf(\"s - Shorten spaces\ \");
printf(\"q - Quit\ \ \");
printf(\"/n Entere your choice: /n\");
scanf(\"%c\",&choice);
switch(choice)
{
case \'c\' :
printf(\"Number of non-whitespace characters: %d\ \ \", GetNumOfNonWSCharacters(userString));
break;
case \'w\' :
printf(\"Number of words: %d\ \ \", GetNumOfWords(userString));
break;
case \'f\' :
printf(\"Edited text: \");
FixCapitalization(userString);
break;
case \'r\' :
printf(\"Edited text: \");
ReplaceExclamation(userString);
break;
case \'s\' :
printf(\"Edited text: \");
ShortenSpace(userString);
break;
case \'q\' :
exit (0);
break;
}
}while(1);
}
int main() {
char userString[256];
printf(\"Enter a sample text:\ \");
scanf (\"%[^\ ]%*c\", userString);
printf(\"\ \");
printf(\"\ You entered: %s\ \",userString);
PrintMenu(userString);
return 0;
}





