2013-04-20 48 views
0

我是C编程新手,尝试编写一个代码来计算字符串中的字数。这里是我的代码,用于计算代码数。C:计算字数(需要帮助修复)

#include<stdio.h> 
    #include<string.h> 
    void main() 
    { 
     int count=0,i,len; 
     char str[100]; 
     printf("enter the sentence"); 
     gets(str); 
     len=strlen(str); 
     for(i=0;i<=len;i++) 
     { 
      if(str[i]==' ') 
       count++; 
     } 
     printf("the number of words are :\t%d",count+1); 
    } 

当我的输入是:Here is four words它工作正常。它给输出 the number of words are : 4

我的问题是我该如何处理“两个连续的空格”二字之间,输入和输入“在最后空间” “开头空间” 。

+3

注意:请不要**使用'gets()'。这是一个安全漏洞,它已经从语言中被弃用,并在下一个标准中被删除。 – WhozCraig 2013-04-20 02:58:39

+0

@WhozCraig我没有意识到这一点;我一直用它来简化它。你能指出我的替代品吗? – 2013-04-20 03:15:39

+0

@WhozCraig我也一直使用gets()。你能指出我使用它的实际缺陷吗? – 2013-04-20 03:32:26

回答

0

您可以使用:

while(str[i]==' '&&str[i]!=EOF) 
{ 
    count++; 
    i++; 
} 

,而不是你的,如果一部分。您还需要在for循环前添加这些代码来读取开始空格。

0

我认为在目前的形式循环可能无法正常工作,

它应该是如下,

for(i=0;i<len;i++) 
    { 
      if(i!=0) 
      { 
      if(str[i]==' ') 
       count++; 
      } 
    } 

要检查其他条件改变代码如下,

for(i=0;i<len;i++) 
    { 
      if(str[i]==' ') 
      { 
       if(i!=0) 
       { 
        if(str[i+1]!=' ') 
        { 
         count++; 
        } 
       } 
    } 
+0

是的,它工作正常,但必须作出一点改变,忽略最后一个空格,以防输入以空格结束。任何方式谢谢 – 2013-04-20 03:01:29

+0

然后将for循环测试更改为i <= n,那么它将正常工作 – Deepu 2013-04-20 03:06:15

0

只需忽略开头的空格和空格紧接在其他空格之后,如果最后没有空格,则为+1。

#include <stdio.h> 
#include <string.h> 
// #include <stdlib.h> 
int main() // void main is a bad practice 
{ 
    int count = 0, i, len, ignoreSpace; 
    char str[100]; 
    printf("enter the sentence\n"); 
    gets(str); 
    len = strlen(str); 
    ignoreSpace = 1; // handle space at the beginning 
    for(i = 0; i < len; i++) // not i<=len 
    { 
     if(str[i] == ' '){ 
      if(!ignoreSpace){ 
       count++; 
       ignoreSpace = 1; // handle two or more consecutive spaces 
      } 
     }else{ 
      ignoreSpace = 0; 
     } 
    } 
    if(!ignoreSpace) // handle space at the last 
     count++; 
    printf("the number of words are :\t%d\n", count); // +1 is moved to previous line 
    // system("pause"); 
    return 0; 
} 
+0

谢谢你,这就是我正在寻找的。关于你对使用int main()而不是void的评论,我是一个初学者,我的讲师正在使用void来教你一些关于它的知识或者只是一个我可以学习的链接 – 2013-04-20 03:11:29

+0

@AatishSai [http:// stackoverflow。 com/a/5296593/2040040] – johnchen902 2013-04-20 03:14:42

0

您应该计算从空间到非空间字符的转换+在开始本身中可能的非空格字符。

#include<stdio.h> 
#include<string.h> 
int main() 
{ 
    int count=0,i,len, cur_is_spc; 
    char str[100]; 
    printf("enter the sentence"); 
    gets(str); 
    len=strlen(str); 

    cur_is_spc = 0;   // 0, if current character is not space. 1, if it is. 

    for(i=0; str[i]!='\0'; i++) 
    { 
     if(str[i] != ' ') 
     { 
      switch(cur_is_spc) // currently holding value for previous character 
      { 
      case 0: count++; break; //count the spc->non-spc transitions 
      case 1:   break; 
      default: cout << "Erroneous value"; exit(1); 
      } 
      cur_is_spc = 1; //updated for current character. 
     } 
     else 
     { 
      cur_is_spc = 0; //updated for current character. 
     } 
    } 

    printf("the number of words are :\t%d",count+1); 
    return 0; 
} 

在这里,我检查只有空格。但可以有像换行符,标签等字符。你的代码如何处理它们?提示:使用isspace()函数。

/此外,该过渡可以从非字母字符字母字符,如果你决定,也就是说是由只字母来完成。这种方法具有内在的灵活性,可以满足您的需求。

0

为什么不干脆用strtok和绕过它:

int main() 
{ 
    int num_words = 0; 
    char str_one[] = "This string has a trailing space "; 
    char str_two[] = " This string has a preceeding space"; 
    char str_three[] = "This string contains two spaces consecutively twice!"; 
    char delim[] = " "; 
    char *ret; 

    /* fgets() for user input as desired... */ 

    if ((ret = strtok(str_one, delim)) != NULL) 
    { 
     while (ret) 
     { 
      num_words++; 
      ret = strtok(NULL, delim); 
     } 
    } 
    else 
    { 
     /* no spaces, but might contain a word if the string isn't empty */ 
     if (str_one[0] != '\0') 
      num_words = 1; 
    } 

    printf("str_one contains %i words\n", num_words); 
    num_words = 0; 

    ... 

    return 0; 
} 

顺便说一句:主要应ALWAYS回报!

0

使用的strtok和strtok的使用的strtok的第一个电话(字符串, “ ”)和呼叫的其余部分使用的strtok(NULL,“ \ n”)

0



一个快速的方法来做到这一点是使用的strtok并根据谓词打破一切。该功能满足您的所有要求。

#include<stdio.h> 
#include<string.h> 
int countSpace(char* str){ 
int counter = 0; 
char * newString; 
newString= strtok (str, " "); // now the newString has the str except first word 
while (newString != NULL){ 
    counter++; // Put counter here to ignore the newString == NULL 
       // Or just -1 from the counter on main() 
    newString= strtok (NULL, " "); //Break the str in to words seperated by spaces 

} 
return counter; 
} 
void main(){ 
    int count=0,i,len; 
    char str[100]; 
    printf("Enter the sentence:\n"); 
    fgets (str , 100 , stdin); 
    count = countSpace(str); 
    printf("The number of words are :\t%d\n",count); 
    return 0; 
} 

谢谢

3

而是计算空间,计算每个单词的第一个非空格字符。

#include<stdio.h> 

int main() 
{ 
    int count=0; 
    char str[100]; 
    printf("enter the sentence"); 
    gets(str); 

    char *cur= str; 

    for (;;) 
    { 
     while (*cur == ' ') 
     { 
      cur++; 
     } 

     if (*cur == 0) 
     { 
      break; 
     } 

     count++; 

     while (*cur != 0 && *cur != ' ') 
     { 
      cur++; 
     } 
    } 

    printf("the number of words are :\t%d",count); 

    return 0; 
}