Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:52:15+00:00 2026-05-27T19:52:15+00:00

I am trying to write a program to find how many 1-letter, 2-letter, 3-letter,

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:52:16+00:00Added an answer on May 27, 2026 at 7:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a program in C language(code::blocks in windows). I have
I am trying to write a program to check that some C source code
I'm trying to write a program that would find the minimum spanning tree. But
I am trying to write a prolog program that can find the tiles that
I'm trying to write a program in Java that when given an MxN matrix
Hii , I have been trying to write a program... We have a structure
I am trying to write a program to find the largest prime factor of
I am trying to write a program in java where in i can find
I'm trying to write some code to automatically configure a bunch of scheduled tasks
I am trying to write a program to find similarity between two documents, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.