Home > Back-end >  Counting words in a String that are separated by other string
Counting words in a String that are separated by other string

Time:09-19

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

CodePudding user response:

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;
}
  •  Tags:  
  • c
  • Related