Question
modify the program to read 3 lines of input, then print the line with the most occurrences of t[] and how many there are. Please
modify the program to read 3 lines of input, then print the line with the most occurrences of t[] and how many there are. Please try not to add any functions and just modify what is given. Thank you!
#include
// getline get a line store it in s - return the size of the read line int getline2(char s[], int lim) { int c, i; //c is char , i is the counters for(i = 0; i < lim-1 && (c = getchar()) != ' '; ++i) { s[i] = c; } s[i] ='\0'; //append the null terminator return i; //return the size }
//function will search the input array for the term array //if we find the term, return the first index of the term within the array //if we dont find the term return -1 flag int strindex(char s[], char t[]) //s input, t term { int i, j, k;
for(i = 0; s[i] != '\0'; i++) //search the array and keeps our place! { for(j = i, k = 0; t[k] !='\0' && s[j] == t[k]; j++ , k++)//j searches where we are starting with i in s, k searches t ; //search for a match!!! if( k > 0 && t[k] == '\0') //if we found a match return i; }
return -1;
} int main() { char line[MAXLINE];
while(getline2(line,MAXLINE) > 0) //keep reading lines until empty line is entered { if(strindex(line,pattern) >= 0) //returns -1 if there is no match printf("%s ", line); }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started