This is C. Please write it C. 1) Prompt the user to enter a string of their choo
ID: 3804356 • Letter: T
Question
This is C. Please write it 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:
Explanation / Answer
your project is really a big one.. hope you like it. please comment if you have any querries
#include<stdio.h>
#define SIZE 200
void main() {
char *string, printMenu(char *), option;
string = malloc(sizeof(char)*SIZE);
gets(string);
do {
option = printMenu(string);
}while (option!='q' && option!='Q');
}
char printMenu(char* input) {
int GetNumOfNonWSCharacters(const char*), GetNumOfWords(char*);
void FixCapitalization(char *), ReplaceExclamation(char *), ShortenSpace(char *);
char option;
printf("MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization r - Replace all !'s s - Shorten spaces q - Quit Choose an option: ");//printing the menu
option = getchar(); //input from standard input
switch(option){
case 'c':
printf("Number of non WS chars: %d ",GetNumOfNonWSCharacters(input));
break;
case 'w':
printf("number of words: %d ",GetNumOfWords(input));
break;
case 'f':
FixCapitalization(input);
printf("Edited string: %s ", input);
break;
case 'r':
ReplaceExclamation(input);
printf("Edited string: %s ", input);
break;
case 's':
ShortenSpace(input);
printf("Edited string: %s ", input);
break;
}
return option;
}
int GetNumOfNonWSCharacters(const char* input) {
int i, count = 0;
for (i = 0; input[i]!=''; i++)
if(input[i]!=' ' && input[i]!=' ' && input[i]!=' ' && input[i]!=' ') //checks for WS
count ++;
return count;
}
int GetNumOfWords(char *input) {
int i, count = 1;
for (i = 0; input[i]!=''; i++)
if(input[i]==' ' || input[i]==' ') //Checks for blanks or tab
count ++; // and count them
return count;
}
void FixCapitalization(char *input) {
int i, flag = 1; //flag is set when a new sentence is starting
for (i = 0; input[i]!=''; i++)
if(input[i]==' ' || input[i]==' ') //ignore blank spaces
continue;
else if(input[i]=='.' || input[i]=='!' || input[i]=='?') //if end of sentence mark it
flag = 1;
else if (flag){
flag = 0; //once we find the first charecter we know that end of sentence effect ends
if (input[i] >='a' && input[i] <='z') //if it is end of sentence and char is lower
input[i] -= 32; //change it to upper case
}
}
void ReplaceExclamation(char *input){ //this method is very simple
int i;
for (i = 0; input[i]!=''; i++)
if(input[i]=='!')
input[i]='.';
}
void ShortenSpace(char *input){
int i, j, flag = 0; //flag here marks a blank space is seen
for (i = 0; input[i]!=''; i++)
if (flag == 1 && (input[i]==' ' || input[i] == ' ')) { //if flag is set and we encounter another blank space we shift the arrray left
for (j = i; input[j]!=''; j++)
input[j] = input[j+1];
i--; //since the array is shorten, we need to change value of i
}
else if(input[i]==' ' || input[i]==' ')
flag = 1; //mark blank space
else
flag = 0; //rset flag if it is not blank space
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.