Question

In: Computer Science

Part 1 Write a program that reads a line of input and display the characters between...

Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters.

For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#!

1) Name your program stars.c.

2) Assume input is no more than 1000 characters.

3) String library functions are NOT allowed in this program.

4) To read a line of text, use the read_line function (the pointer version) in the lecture notes.

5) Include and call the search function. The search function expects s1 to point to a string containing the input as a string and stores the characters between the first two '*' to the string pointed by s2. If the input does not contain two '*', s2 should contain an empty string. An empty string is a valid string with no characters except the null character. The function returns 1 if the input contains two '*', and returns 0 otherwise.

int search (char *s1, char *s2);

6) The search function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.

7) The main function should display the output

comments and steps would be helpful Thanks

Part 2 is to modify part 1 of the program so that input comes in as command line arguments.

Sample run: ./a.out W8 4 ME 2 finish 2!

Output: Consonants: WMfnsh

Solutions

Expert Solution

"stars.c" code screenshot:

"stars.c" code:

#include <stdio.h>

// this function is already given
int read_line(char *str, int n)
{
    int ch, i = 0;
    while ((ch = getchar()) != '\n')
    {
        if (i < n)
        {
            *str++ = ch;
            i++;
        }
    }
    *str = '\0';
    return i;
}

int search(char *s1, char *s2)
{
    // First we have to find if the input string contains
    // atleast two '*'. If not found then return 0.
    int cntStar = 0;
    // when we will find two '*' then break the loop
    while (*s1 != '\0' && cntStar < 2)
    {
        if (*s1++ == '*')
            cntStar++;
    }
    // if atleast two '*' not found then return 0 with
    // empty string with null
    if (cntStar < 2)
    {
        *s2 = '\0';
        return 0;
    }
    else
    {
        // Here we will find the characters between the first two '*'

        // Currently s1 is not pointing first '*' character
        // So, we need to move back using decrement operator
        while (cntStar > 0)
        {
            s1--;
            if (*s1 == '*')
                cntStar--;
        }

        int isFirstStar = 0;
        while (*s1 != '\0')
        {
            // if below condition is met, then character is our first '*'
            if (*s1 == '*' && isFirstStar == 0)
            {
                isFirstStar = 1;
            }
            // if below condition is met, then character is our second '*'
            // and hence we will return to main() function
            else if (*s1 == '*' && isFirstStar == 1)
            {
                *s2 = '\0';
                return 1;
            }
            // if we encountered first '*' character previously
            // then keep storing the characters in s2
            else if (isFirstStar == 1)
            {
                *s2++ = *s1;
            }
            // increment to get next character
            s1++;
        }
        return 1;
    }
}

int main()
{
    char str1[1000];
    char str2[1000];
    int size, isValid;

    printf("Enter input string: ");
    size = read_line(str1, 1000);
    isValid = search(str1, str2);

    if (isValid == 0)
        printf("\nUnable to find two '*' !!!");
    else
        printf("\nOutput: %s", str2);
    return 0;
}

Output:


Related Solutions

Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a program that prompts the user to enter two characters and display the corresponding major...
Write a program that prompts the user to enter two characters and display the corresponding major and year status. The first character indicates the major. And the second character is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. We consider only the following majors: B (or b): Biology C (or c): Computer Science I (or i): Information Technology and Systems M (or m): Marketing H (or h): Healthcare Management...
MARS: Use MASKING to convert ASCII characters to Integer Write and run a program that reads...
MARS: Use MASKING to convert ASCII characters to Integer Write and run a program that reads in a string of ASCII characters and converts them to Integer numbers stored in an array USING MASKING, not subtraction. Write a program that: 1. Inputs a 1x8 vector of single-digit integers 2. Stores them into an 8-entry 32-bit Integer array, “V”. After storing the integers in the array: 1. Read the same values using Read Integer and store them in a 32-bit integer...
Question 1: Write an AVR program to display text on a 2-line WH2002 LCD display based...
Question 1: Write an AVR program to display text on a 2-line WH2002 LCD display based on the HD44780 controller. Apply the following: The LCD instruction/data bus is connected to PORTD, and control bus is connected to PORTB (RS is connected to PB0, R/W to PB1, E to PB2). Use the #define command to name PORTD as Display_port. Use the #define command to name PORTB as Control_port. The displayed information is stored in the microcontroller Flash ROM (maximum size of...
Input the available amount of Rice in kg. Write a program that reads a sequence of...
Input the available amount of Rice in kg. Write a program that reads a sequence of Rice donations in kg and add them to the initially available amount of Rice. The loop should stop once the accumulated amount of Rice exceed 50 kg. Then, the program should print the final amount of Rice and the number of considered donations.
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT