Write a C language program that reads input until it encounters a ‘#’ character.
ID: 3723730 • Letter: W
Question
Write a C language program that reads input until it encounters a ‘#’ character. Count all the occurrences of the letter sequence “is” in that order.
A screenshot showing the series of values tested.
Test a minimum of 7 cases including:
“is this working#” (result -> 2) “wishing is what wishers do#” (result -> 3) “Mississippi#” (result -> 2) “Missouri#” (result -> 1) “isiissiiisss#” (result-> 3) “ssss si s i#” (result -> 0) “i s iiiiii#” (result -> 0) A listing of your C program Be sure your name and CWID is printed by the program. Test with a minimum of the cases I have presented, more is better.
Notes: This problem does not break easily into functions, for this assignment, writing all the logic in “main” is acceptable.
Explanation / Answer
#include <stdio.h>
#include <string.h>
char str[100], sub="is";
int count = 0, count1 = 0;
void main()
{
int i, j, l, l1, l2;
//Get the input string from user
printf(" Enter a string : ");
scanf("%[^ ]s", str);
l1 = strlen(str);
l2 = strlen(sub);
//check for thr occurance of 'is' in the string
for (i = 0; i < l1;)
{
j = 0;
count = 0;
while ((str[i] == sub[j]))
{
count++;
i++;
j++;
}
if (count == l2)
{
count1++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times in %s", sub, count1, str);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.