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 a line of text input by the user and places each...
Write a program that reads a line of text input by the user and places each word in a TreeSet. Print the elements of the TreeSet to the screen. This will cause the elements to be printed in ascending order. Using Eclipse for this. TreeSetUse.java.
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in...
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in 20 numbers. A method with an int parameter should display whether the number is odd or even for the number passed
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 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...
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...
Use fork() Create a program that reads characters from an input file. Use fork to create...
Use fork() Create a program that reads characters from an input file. Use fork to create parent and child processes. The parent process reads the input file. The input file only has letters, numbers. The parent process calculates and prints the frequency of the symbols in the message, creates the child processes, then prints the information once the child processes complete their execution. The child processes receives the information from the parent, generates the code of the assigned symbol by...
Write a program that reads in a single integer. Display a message depending on what is...
Write a program that reads in a single integer. Display a message depending on what is given: When the number is 42, output "42 is the Ultimate Question of Life, the Universe, and Everything." When the number is 2020 output "2020 is the current year." When the number is greater than 1000 and less than 3000 output "1500 could be a year." where 1500 is the number the user originally inputted When none of the above, output "no idea." One...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT