C 计算在给定句子中组成单词的字母

C 计算在给定句子中组成单词的字母,c,c-strings,C,C Strings,我正试图写一个程序来找出一个给定的句子中有多少个1字母、2字母、3字母、4字母的单词,我终于找到了一些代码。然而,有一个问题。代码已成功编译,但在运行时,程序失败并退出,没有结果 int main( void ) { char *sentence = "aaaa bb ccc dddd eee"; int word[ 5 ] = { 0 }; int i, total = 0; // scanning sentence for( i = 0; *( sentence + i ) != '\0'

我正试图写一个程序来找出一个给定的句子中有多少个1字母、2字母、3字母、4字母的单词,我终于找到了一些代码。然而,有一个问题。代码已成功编译,但在运行时,程序失败并退出,没有结果

int main( void )
{
char *sentence = "aaaa bb ccc dddd eee";
int word[ 5 ] = { 0 };
int i, total = 0;

// scanning sentence
for( i = 0; *( sentence + i ) != '\0'; i++ ){
    total = 0;

    // counting letters in the current word
    for( ; *( sentence + i ) != ' '; i++ ){
        total++;
    } // end inner for

    // update the current array
    word[ total ]++;
} // end outer for

// display results
for( i = 1; i < 5; i++ ){
    printf("%d-letter: %d\n", i, word[ i ]);
}

system("PAUSE");
return 0;
} // end main 
int main(无效)
{
char*句=“aaaa bb ccc dddd eee”;
int字[5]={0};
int i,总计=0;
//扫描句
对于(i=0;*(句子+i)!='\0';i++){
总数=0;
//计算当前单词中的字母
对于(;*(句子+i)!='';i++){
总计++;
}//内端为
//更新当前阵列
字[总数]+;
}//外部结束
//显示结果
对于(i=1;i<5;i++){
printf(“%d-字母:%d\n”,i,单词[i]);
}
系统(“暂停”);
返回0;
}//末端总管

你在最后一个词后面分词。内部循环到达空终止符时不会终止

$ gcc -g -o count count.c
$ gdb count
GNU gdb (GDB) 7.3-debian
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/nathan/c/count...done.
(gdb) run
Starting program: /home/nathan/c/count 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004005ae in main () at count.c:9
9       for( i = 0; *( sentence + i ) != '\0'; i++ ){
(gdb) p i
$1 = 772
$gcc-g-o count.c
$gdb计数
GNU gdb(gdb)7.3-debian
版权所有(C)2011免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“x86_64-linux-gnu”。
有关错误报告说明,请参阅:
...
从/home/nathan/c/count读取符号…完成。
(gdb)运行
启动程序:/home/nathan/c/count
程序接收信号SIGSEGV,分段故障。
计数处主()中的0x00000000004005ae.c:9
9表示(i=0;*(句子+i)!='\0';i++){
(gdb)p i
$1 = 772
其他注释:为什么要在末尾调用
系统(“暂停”)
?请确保使用
-Wall
#包含所用库的
标题进行编译。即使它们是标准库的一部分。

#包含
#include <stdio.h>

int main( void ){
    char *sentence = "aaaa bb ccc dddd eee";
    int word[ 5 ] = { 0 };
    int i, total = 0;

    // scanning sentence
    for( i = 0; *( sentence + i ) != '\0'; i++){
        total = 0;

        // counting letters in the current word
        for( ; *( sentence + i ) != ' '; i++ ){
            if(*(sentence + i) == '\0'){
                --i;
                break;
            }
            total++;
        } // end inner for

        // update the current array
        word[ total-1 ]++;
    } // end outer for

    // display results
    for( i = 0; i < 5; i++ ){
        printf("%d-letter: %d\n", i+1, word[ i ]);
    }

    system("PAUSE");
    return 0;
} // end main 
内部主(空){ char*句=“aaaa bb ccc dddd eee”; int字[5]={0}; int i,总计=0; //扫描句 对于(i=0;*(句子+i)!='\0';i++){ 总数=0; //计算当前单词中的字母 对于(;*(句子+i)!='';i++){ 如果(*(句子+i)='\0'){ --一,; 打破 } 总计++; }//内端为 //更新当前阵列 字[total-1]++; }//外部结束 //显示结果 对于(i=0;i<5;i++){ printf(“%d-字母:%d\n”,i+1,单词[i]); } 系统(“暂停”); 返回0; }//末端总管