C Programming Code Examples C > Strings Code Examples C Program To Count the Occurence of a Substring in String C Program To Count the Occurence of a Substring in String This program takes a string and a substring as input and counts the occurrence of a substring in string. - Take a string and a substring as input. - Firstly check for the substring in the string. - If it is present then count the number of times it is present. #include <stdio.h> #include <string.h> char str[80], sub[80]; int count = 0, count1 = 0; void main() { int i, j, l, l1, l2; printf("\nEnter a string : "); scanf("%[^\n]s", str); l1 = strlen(str); printf("\nEnter a substring : "); scanf(" %[^\n]s", sub); l2 = strlen(sub); 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); }