C 数一串字

C 数一串字,c,string,algorithm,count,C,String,Algorithm,Count,正如标题所述,我必须补充几点来解释:“/tab/”,“/”是在我的情况下划分单词的东西,另一个注意点是,一个空格或点可以一个接一个地出现 这就是我所拥有的 int countWords(char * str, int length){ int counter = 0; for( int i = 0; i < length; i++){ if(( str[i] == " ") || ( str[i] == ".") || ( str[i

正如标题所述,我必须补充几点来解释:
“/tab/”,“/”
是在我的情况下划分单词的东西,另一个注意点是,一个空格或点可以一个接一个地出现

这就是我所拥有的

int countWords(char * str, int length){
       int counter = 0;

        for( int i = 0; i < length; i++){
            if(( str[i] == " ") || ( str[i] == ".") || ( str[i] == ",")){
                if(( str[i+1] != " ") || ( str[i+1] != ".") || (str[i+1] != ",")){
                    if(( str[i-1] != " ") || ( str[i-1] != ".") || (str[i-1] != ","))
                        counter++;
                }
            }
        }
        return counter;
    }
int countWords(字符*str,int长度){
int计数器=0;
for(int i=0;i
我得到一个错误,说我不能比较int和指针,我确实知道它是从哪里来的,但是我如何才能让它真正工作呢


注意*我不能在这样的表达式中使用
string.h

str[i] == " "
          ^^^
您正在尝试将
char
类型的对象与隐式转换为
char*
类型的字符串文本进行比较

必须使用字符常量而不是字符串文本

str[i] == ' '
          ^^^
封闭的if语句没有意义。例如,对于
i
等于0,表达式
str[i-1]
尝试访问字符串以外的内存

函数的第一个参数应该用限定符
const
声明

如演示程序所示,该函数可以按以下方式显示

#include <stdio.h>

size_t countWords( const char *s ) 
{
    size_t n = 0;

    for (size_t i = 0; s[i] != '\0'; )
    {
        while (s[i] == ' '  || 
               s[i] == '\t' || 
               s[i] == '.'  || 
               s[i] == ',') ++i;

        if (s[i] != '\0')
        {
            ++n;
            while ( s[i] != '\0' &&
                    ! ( s[i] == ' '  ||
                        s[i] == '\t' ||
                        s[i] == '.'  ||
                        s[i] == ',')) ++i;

        }
    }

    return n;
}

int main( void )
{
    char *s = "  Hello\t, World...";

    printf("%zu\n", countWords(s));
}
或者函数的实现可以如下所示

size_t countWords(const char *s)
{
    size_t n = 0;

    while ( *s )
    {
        while (*s == ' '  ||
               *s == '\t' ||
               *s == '.'  ||
               *s == ',') ++s;

        if ( *s )
        {
            ++n;
            while ( *s &&
                    !( *s == ' '  ||
                       *s == '\t' ||
                       *s == '.'  ||
                       *s == ',' ) ) ++s;

        }
    }

    return n;
}
声明函数的更通用方法如下

size_t countWords( const char *s1, const char *s2 );

其中字符串
s2
指定了一组单词分隔符。

如果要计算单词数,K&R ANSI C中有一个很好的例子。请尝试编写这样的程序

#include <stdio.h>
#define IN  1    /* inside a word */
#define OUT 0    /* outside a word */
/* count lines, words, and characters in input */
main()
{
   int c, nl, nw, nc, state;
   state = OUT;
   nl = nw = nc = 0;
   while ((c = getchar()) != EOF)
 {
    ++nc;
    if (c == '\n')
        ++nl;
    if (c == ' ' || c == '\n' || c = '\t')
        state = OUT;
    else if (state == OUT) 
    {
        state = IN;
        ++nw;
     }
   }
printf("%d %d %d\n", nl, nw, nc);
}
#包括
#在1/*中定义一个单词*/
#在单词外定义0/**/
/*计算输入中的行、字和字符数*/
main()
{
北卡罗来纳州、北卡罗来纳州、北卡罗来纳州、北卡罗来纳州;
状态=输出;
nl=nw=nc=0;
而((c=getchar())!=EOF)
{
++数控;
如果(c=='\n')
++nl;
如果(c=''| | c='\n'| | c='\t')
状态=输出;
else if(state==OUT)
{
状态=IN;
++西北;
}
}
printf(“%d%d%d\n”,nl,nw,nc);
}

希望这有帮助。:)你可以找到这本书的pdf格式

最好不要使用
string.h
。但请使用
字符串
标题。:)
str[i]==“”
不会做你认为它会做的事情。@πάνταῥεῖ 所以,它不计算空间,我还应该做什么?@ AHMADKHATETEB使用字符文字来代替:<代码> STR[i]=='/COD> >我想你可以从这个问题中得到一个很好的解决方案,如此丑陋,你可以考虑使用<代码> StReST()/代码>。但问题指出,不允许使用任何字符串函数,无论它们是来自标准头还是来自特定于实现的头。@ChronoKitsune奇怪的要求。。。虽然还可以。
#include <stdio.h>
#define IN  1    /* inside a word */
#define OUT 0    /* outside a word */
/* count lines, words, and characters in input */
main()
{
   int c, nl, nw, nc, state;
   state = OUT;
   nl = nw = nc = 0;
   while ((c = getchar()) != EOF)
 {
    ++nc;
    if (c == '\n')
        ++nl;
    if (c == ' ' || c == '\n' || c = '\t')
        state = OUT;
    else if (state == OUT) 
    {
        state = IN;
        ++nw;
     }
   }
printf("%d %d %d\n", nl, nw, nc);
}