繁体   English   中英

计算字符串中由其他字符串分隔的单词

[英]Counting words in a String that are separated by other string

我必须使用其他字符串分隔符来计算字符串中的单词。 例如,如果我有一个字符串"akjvnrupajcruamvoq"和分隔符"ru" ,它必须返回 2,如果我有"dadadadada"和分隔符"da" ,它必须返回 0。在找到分隔符后,我无法计算单词。

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

另一种方法是使用现有的 function ( strstr ) 来搜索分隔符。 strtok可能是另一种选择,但我不确定它是否完全符合您的要求。

转念一想,这看起来有点太模糊了......

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

尝试这个。
注释行应该解释正在发生的事情。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM