Q:

counting letters that make up words in a given sentence

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 

A:

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.

