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 java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
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 in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
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.
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT