String Matched and returning only the count, C Program to count the substring from the main String
C Program to count the substring from the main String
Input 1 : "AGCVbjasudsfabcaugisabcaabcAA"
Input 2 : "abc"
Output : 3
Input 2 : "abc"
Output : 3
C Program:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size
/* Function declaration */
int countOccurrences(char * str, char * toSearch);
int main()
{
char str[MAX_SIZE];
char toSearch[MAX_SIZE];
int count;
/* Input string and word from user */
printf("Enter any string: ");
gets(str);
printf("Enter word to search occurrences: ");
gets(toSearch);
count = countOccurrences(str, toSearch);
printf("Total occurrences of '%s': %d", toSearch, count);
return 0;
}
/**
* Get, total number of occurrences of a word in a string
*/
int countOccurrences(char * str, char * toSearch)
{
int i, j, found, count;
int stringLen, searchLen;
stringLen = strlen(str); // length of string
searchLen = strlen(toSearch); // length of word to be searched
count = 0;
for(i=0; i <= stringLen-searchLen; i++)
{
/* Match word with string */
found = 1;
for(j=0; j<searchLen; j++)
{
if(str[i + j] != toSearch[j])
{
found = 0;
break;
}
}
if(found == 1)
{
count++;
}
}
return count;
}
I love this!! Definitely will be keeping this idea in mind!
ReplyDeleteDOT NET Training in Chennai
.Net training in chennai
DOT NET Course in Chennai
Html5 Training in Chennai
Html5 Courses in Chennai
DOT NET Training in Chennai
DOT NET Training Institute in Chennai