I have to count the words in a string using other string separator. For example if I have a string "akjvnrupajcruamvoq" and separator "ru" it must return 2 and if I have "dadadadada" and a separator "da" it must return 0. I have troubles with counting the words after I found a separator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int wc(char* pcSource, char* pcSeparator)
{
int iWordsCount=0;
int iI=0;
int iJ=0;
int iK=0;
int iCharCount=0;
int iLenghtOfSeparator=strlen(pcSeparator);
int iLenghtOfSource=strlen(pcSource);
for (iI; iI < iLenghtOfSource; iI++)
{
iK=iI;
if ((iK != 0) && (iK != iLenghtOfSource - iLenghtOfSeparator))
{
while (iJ < iLenghtOfSeparator)
{
if (pcSource[iK] == pcSeparator[iJ])
{
iK++;
}
if (pcSource[iK] != pcSeparator[iJ])
{
iCharCount++;
iK++;
}
iJ++;
}
}
outOfLoop:
if (iCharCount == iLenghtOfSeparator)
{
iWordsCount++;
}
iJ=0;
iCharCount=0;
}
return iWordsCount;
}
int main()
{
int iMaxSize=2048;
char pcSource[]="cocacola";
char pcSeparator[]="co";
int iWordsCount;
iWordsCount = wc(pcSource,pcSeparator);
printf("Words count in this string are: %d",iWordsCount);
return 0;
}