In: Computer Science
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
"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: