Question

In: Computer Science

C code please (1) Prompt the user to enter a string of their choosing. Store the...

C code please

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:

Enter a sample text:
we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  nothing ends here;  our hopes and our journeys continue!

You entered: we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  nothing ends here;  our hopes and our journeys continue!


(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:

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:


(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:

Number of non-whitespace characters: 181


(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:

Number of words: 35


(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:

Edited text: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!


(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.

Edited text: we'll continue our quest in space.  there will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  nothing ends here;  our hopes and our journeys continue.


(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:

Edited text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue!

#include<stdio.h>
#include <string.h>

int main(void) {

/* Type your code here. */

return 0;
}

Solutions

Expert Solution

Answer 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string.

#include <studio.h>
#include<conio.h>
void main()
{
char userText[];
  
printf( "Enter text \n");
scanf("%s", &userText);
printf("You entered: %s", userText);

getchar();
return 0;
}

Answer 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.

#include <studio.h>
#include<conio.h>
void main()
{
char userText[];
  
printf( "Enter text \n");
scanf("%s", &userText);
printf("You entered: %s", userText); int printMenu (userMenu[])
{
printf("MENU \n");
printf("c - Number of non-whitespace characters \n");
printf("w - Number of words \n");
printf("f - Fix capitalization\n");
printf("r - Replace all !'s \n");
printf("s - Shorten spaces \n");
printf("q - Quit \n");
printf("Choose an option: %d \n " userChar);
scanf ("%d", &userChar);
  
if (userChar == 'q')
{
printf("q");
printf("Quit");
}
else if (userChar == 'w')
{
printf("w");
printf("Number of words: ");
}
else if (userChar == 'f')
{
printf("f");
printf("Fix capitalization ");
}
else if (userChar == 'r')
{
printf("r");
printf("Replace all !'s: ");
}
else if (userChar == 's')
{
printf("s");
printf("Shorten spaces: ");
}
else if (userChar == 'c')
{
printf("c");
printf("Number of non-whitespace characters: ");
}
else
{
printf("Please select a valid character.");
}

return userChar[];
}
Answer 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.

#include <studio.h>
#include<conio.h>
int GetNumOfNonWSCharacters()
{
char userText[];
  
printf( "Enter text \n");
scanf("%s", &userText);
printf("You entered: %s", userText); GetNumOfNonWSCharacters()
{
int i, count=0, totalwords=0;
int nonWS=0;
for(i=0; userText[i]; i++)

{

totalwords++
if( userText[i] == ' ')
count++;
}

nonWS= totalwords-count;

printf("\nTotal blank spaces : %d", nonWS);
getch();
}

Answer 4 )

Implement the GetNumOfWords() function. GetNumOfWords() has a constant string as a parameter and returns the number of words in the string.

int GetNumOfWords()
{
int i, count=0;
  
for(i=0; userText[i]; i++)
if( userText[i] == ' ')
count++;
printf("\nTotal number of words : %d", count-1);
getch();
}

Answer 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.
int FixCapitalization()
{
   int i;

   for(i=0; userText[i]!='\0'; i++)
   {
       if(i==0)
       {
           if((userText[i]>='a' && userText[i]<='z'))
               userText[i]=userText[i]-32;
           continue;
       }
       if(userText[i]==' ')
       {
           ++i;
           if(userText[i]>='a' && userText[i]<='z')
           {
               userText[i]=userText[i]-32;
               continue;
           }
       }
       else
       {
           if(userText[i]>='A' && userText[i]<='Z')
               userText[i]=userText[i]+32;
       }
   }
  
   printf("Capitalize userTexting is: %s\n", userText);
  
   return 0;
}
Answer 6)

Implement the ReplaceExclamation() function. ReplaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character.

int ReplaceExclamation()
{
int i;
  
for(i=0; userText[i];i++)
if(userText[i] == '!')
userText[i]= '.'
  
printf("Exclamation Replaced%d", userText);

getch();
}

Answer 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.  

int shortenString()
{
int i;
for(i=0; userText[i]==' '|| userText[i]=='\t';i++);

for(j=0; userText[i]; i++)

{

userText[j++]=userText[i];

}

userText[j]='\0';

printf("\nAfter trimming leading white spaces:%s",s);

getch();

}


Related Solutions

This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Part I: Prompt the user for a single string and store it in a variable named...
Part I: Prompt the user for a single string and store it in a variable named userString. a. Use a for loop to print the string, char by char, with a dash '-' char between each. b. Use a for loop to print the string backwards, char by char, with a dash '-' char between each. Part II: Create an array of 5 strings named userStrings. Use a generalized array size. const int n = 5; string userStrings[n]; a. Populate...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
use c++ (1) Prompt the user for a string that contains two strings separated by a...
use c++ (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex:...
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter....
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter. You can assume that the the length of this message will be less than 100 characters. You will then parae this message, character by character, converting them to lower case, and find corresponding characters in the words found in the key text word array. Once a character match is found, you will write the index of the word and the index of the character...
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in...
(1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 142.0 Enter weight 4: 166.3 Enter weight 5: 93.0 You entered: 236.0 89.5 142.0 166.3 93.0 (2) Also output the total weight, by summing the array's elements. (1 pt) (3) Also output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT