Counting words in a String that are separated by other string

Viewed 79

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;
} 
2 Answers

An alternative approach is to use an existing function (strstr), to search for the separator. strtok may be an alternative, but I'm not sure it fits your requirements completely.

On second thought this looks a little too obfuscated...

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int wc(const char *s, const char *sep)
{
    int seplen = strlen(sep);
    int count = 0;
    const char *p = s;        // last place where I found a separator
    while (p && *s) {         // if I found a separator and there are still characters to check
        p = strstr(s, sep);   // look for a separator
        count += !p || p > s; // if I didn't found one or found one with some characters in between, there is a word
        s = p + seplen;       // continue searching after current separator
    }
    return count;
}


int main(void)
{
    char *test[][2] = {
        { "cocacola", "co" },
        { "dadadad", "dad" },
        { "xdadadxad", "dad" },
        { "xdadadadx", "dad" },
    };

    for (size_t i = 0; i < 4; ++i) {
        printf("Words count in \"%s\" is %d\n", test[i][0], wc(test[i][0],test[i][1]));
    }

    return 0;
}

Try this.
Commented lines should explain what is happening.

#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 iStartSubStr = 0;

    while ( pcSource[iI]) // not terminating zero
    {
        iJ = 0;
        if ( pcSource[iI] == pcSeparator[iJ]) // start of separator
        {
            iK = 0;
            while ( pcSeparator[iJ]) // not terminating zero
            {
                if (pcSource[iK + iI] != pcSeparator[iJ]) // not the same
                {
                    break;
                }
                iK++;
                iJ++;
            }
            if ( ! pcSeparator[iJ]) // terminating zero
            {
                if ( iStartSubStr != iI) // skip if substring begins with separator
                {
                    ++iWordsCount;
                }
                iStartSubStr = iI + iK; // set start of next sub string
                iI += iK;
            }
            else
            {
                ++iI;
            }
        }
        else
        {
            ++iI;
        }
    }
    if ( pcSource[iStartSubStr]) // last sub string
    {
        ++iWordsCount;
    }

    return iWordsCount;
}

int main()
{
    char pcSource[]="cocacola";
    char pcSeparator[]="co";
    int iWordsCount;

    iWordsCount = wc(pcSource,pcSeparator);
    printf("Words count in this string are: %d\n",iWordsCount);

    return 0;
}
Related