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

发布于 2024-12-24 16:14:21 字数 661 浏览 0 评论 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 

I am trying to write a program to find how many 1-letter, 2-letter, 3-letter, 4-letter words exist in a given sentence, and I have finally come up with some code. However, there is a problem. The code has been successfully compiled, but when it comes to running, the program fails and quits with no result.

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2024-12-31 16:14:21

你在最后一个词之后出现了段错误。当到达空终止符时,内部循环不会终止。

$ 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

其他评论:为什么最后要调用system("PAUSE")?确保使用您使用的库的 -Wall#include 标头进行编译。即使它们是标准库的一部分。

You're segfaulting after the last word. The inner loop doesn't terminate when it gets to the null terminator.

$ 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

Other comments: Why call system("PAUSE") at the end? Make sure you compile with -Wall and #include headers for the libraries you use. Even if they're part of the standard library.

暗恋未遂 2024-12-31 16:14:21
#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 
#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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文