Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, substr_count, that counts the occurrences of the subString \"c

ID: 3787779 • Letter: W

Question

Write a function, substr_count, that counts the occurrences of the subString "cpre288" in an input string, srcString. #include #include//YOU CAN USESTRING LIBRARY Functions here//Count the number of occurrences of "cpre288" in srcString int substr_count(char * srcString, char * subString) {//YOURCODE HERE int main(void) {char srcString[200] = "";//empty string char subString[8]] = "cpre288";//subString int get_count = 0; scanf("%s", srcString);//get a string from input keyboard get_count = substr count(srcString, substring); printf("%s occurs %d times in %s ", substring, get_count, srcString);}

Explanation / Answer

#include <stdio.h>
#include <string.h>
int substr_count(char *srcString, char *subString){
int i,j, length1, length2, count = 0, substringCount = 0;
length1 = strlen(srcString);
length2 = strlen(subString);
for (i = 0; i < length1;)
{
j = 0;
count = 0;
while (i < length1 && (srcString[i] == subString[j]))
{
count++;
i++;
j++;
}
if (count == length2)
{
substringCount++;   
count = 0;
}
else
i++;
}
return substringCount;
  
}

int main()
{
char srcString[200] = "";
char subString[8] = "cpre288";
int get_count = 0;
scanf("%s", &srcString);
get_count = substr_count(srcString, subString);
  
printf("%s occurs %d times in string %s ", subString, get_count, srcString);
  

return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

cpre288sureshcpre288anshucpre288                                                                                                                                                                                                                       

cpre288 occurs 3 times in string cpre288sureshcpre288anshucpre288

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote