Question

In: Computer Science

strcpy strcat strcmp strstr strchr fgets Specification The program should repeatedly prompt the user to enter...

  • strcpy
  • strcat
  • strcmp
  • strstr
  • strchr
  • fgets

Specification

The program should repeatedly prompt the user to enter a string of no more than 20 characters. The program will end when the user types in DONE (all caps). Use fgets to get input from the user.

Because fgets leaves the newline character on the string, the program should trim off the newline by replacing it with the null character, effectively shortening the string. Use strchr to locate the newline.

The program will use the input to build up a longer string. You may assume this string will be no longer than 1000 characters. The user input is compared to the longer string and then takes action as follows:

  • If the input is contained within the longer string, ignore it. Use strstr to perform the search.
  • If the user input is less than the longer string (as indicated by the return value from strcmp), prepend the input onto the longer string with a space separating them.
  • If the input is greater than the longer string, append it with a space separating them.

After each user input, display the longer string.

Sample Run

Here's a sample of how the program would work. I've shown user input in bold, just to help it stand out. Your program won't use any special typography.

Enter a string: hello
hello
Enter a string: world
hello world
Enter a string: bye
bye hello world
Enter a string: cat
bye hello world cat
Enter a string: worl
bye hello world cat
Enter a string: beep
beep bye hello world cat
Enter a string: bingo
beep bye hello world cat bingo
Enter a string: DONE
beep byte hello world cat bingo

Let's analyze this one input at a time.

  1. User enters hello. Since this is the first sting entered, it is copied into the longer string.
  2. User enters world. Because world comes after hello, it is appended onto the longer string.
  3. User enters bye. This comes before hello world, so it is prepended.
  4. User enters cat. This comes after bye hello world, and is appended.
  5. User enters worl. This string is found within the longer one; no change to the longer string.
  6. User enters beep. Since beep comes before bye hello world cat, it is prepended.
  7. User enters bingo. This comes after beep bye hello world cat, and is appended.
  8. User enters DONE. The longer string is displayed one last time and the program ends.

Please note that the program is not putting the words in alphabetic order. It's actually doing something simpler than sorting.

Solutions

Expert Solution

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

int main()
{
    //Define a variable for input and output string*/
    char input[20];
    char output[1000] = "";

    // loop for getting input string
    while(1)
    {
        printf("Enter a string: ");
        // read input string
        if (fgets(input, sizeof(input), stdin) == NULL)
        {
            printf("Fail to read the input string");
        }
        else
        {
            //find new line
            char *ptr = strchr(input, '\n');
            if (ptr)
            {
                //if new line found replace with null character
                *ptr = '\0';
            }
        }

        // if input is DONE exit loop
        if (strcmp(input, "DONE") == 0)
            break;

        // If the input is contained within the longer output, ignore it and restart loop
        if(strstr(output, input)!=NULL )
        {
            printf("%s\n", output);
            continue;
        }

        // if input is the first sting entered, it is copied into the output string
        else if(strlen(output)==0)
            strcpy(output, input);


        // else if input is greater than the output string , append the input onto the output string with a space separating them.
        else if(strcmp(output, input)<0)
        {
            strcat(output, " ");
            strcat(output, input);
        }

        // else if input is less than the output string , prepend the input onto the output string with a space separating them.
        else if(strcmp(output, input)>=0)
        {
            strcat(input, " ");
            char temp[1000] = "";
            strcpy(temp, input);
            strcat(temp, output);
            strcpy(output, temp);
        }

        printf("%s\n", output);

   }
   return 0;
}


Output


Related Solutions

In.java Write a program that repeatedly asks the user to enter their password until they enter...
In.java Write a program that repeatedly asks the user to enter their password until they enter the correct one. However, after 5 failed attempts, the program "locks out" the user. We will show that with an error message. Assume the password is HOC2141 (case sensitive). Note that there is a special case that is not shown below. To identify it, think of all possible scenarios of input for this program. ----------- Sample run 1: Enter your password: Blake Wrong Enter...
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...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
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...
Write a C program that repeatedly prompts the user for input at a simple prompt (see...
Write a C program that repeatedly prompts the user for input at a simple prompt (see the sample output below for details). Your program should parse the input and provide output that describes the input specified. To best explain, here's some sample output: ibrahim@ibrahim-latech:~$ ./prog1 $ ls -a -l -h Line read: ls -a -l -h Token(s): ls -a -l -h 4 token(s) read $ ls -alh Line read: ls -alh Token(s): ls -a -l -h 2 token(s) read $...
(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or...
(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or enter -1 to stop. When finished entering all the scores, the program should display the number of scores entered, the sum of the scores, the mean, the lowest and the highest score. (IN PYTHON)
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Answer in JAVA Write a program that would prompt the user to enter an integer. The...
Answer in JAVA Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT