Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Simple C program to count word lengths

Programming


Tags
beginner, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-12-2016
Riker1204 Riker1204 is offline
Registered User
 
Join Date: May 2012
Last Activity: 14 October 2016, 4:40 PM EDT
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Simple C program to count word lengths

So my program is not working and I keep changing it to figure out why. So I have two questions, can I do tracing similar to bash, and also what is wrong with this.

The idea is simple, I want to count "word" lengths, with the loose definition of word not being a space, tab, or newline. Here is what I wrote to test if it is counting words by outputting the number of 3 letter words, but it always gives 0.


Code:
#include <stdio.h>

main() {

     int c, i, state, wl;
     int maxlength[15];

     #define IN 1
     #define OUT 0

     wl = i = c = 0;
     state = OUT;
    
     for (i=0; i < 15; ++i)
          maxlength[i] = 0;

     while ((c = getchar()) != EOF) {
          if (c == ' ' || c == '\t' || c == '\n') {
               if (state == OUT)
                    ;
               else if (state == IN) {
                    state == OUT;
                    ++maxlength[wl];
                    wl = 0;
               }
          else { 
               ++wl;
               state = IN;
          }
          }
     }
     printf("Number of 3 letter words: %d\n", maxlength[3]); 
}

Sponsored Links
    #2  
Old Unix and Linux 10-12-2016
disedorgue disedorgue is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 19 January 2017, 6:55 PM EST
Location: France
Posts: 439
Thanks: 36
Thanked 166 Times in 154 Posts
Hi,
Already, you have typo:

Code:
               else if (state == IN) {
                    state == OUT;
                    ++maxlength[wl];
                    wl = 0;

Regards.
The Following User Says Thank You to disedorgue For This Useful Post:
Riker1204 (10-12-2016)
Sponsored Links
    #3  
Old Unix and Linux 10-12-2016
Riker1204 Riker1204 is offline
Registered User
 
Join Date: May 2012
Last Activity: 14 October 2016, 4:40 PM EDT
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
Thanks! I'm always good for a few typos...

So I corrected the typo and recompiled and re ran a test, and still a perpetual zero result when supplied with 3 letter words.

My logic is very wrong somewhere.
    #4  
Old Unix and Linux 10-12-2016
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 January 2017, 5:50 PM EST
Location: Saskatchewan
Posts: 21,760
Thanks: 1,027
Thanked 4,032 Times in 3,740 Posts
You are getting into the if(c == '\n') section with state as still OUT and thereby bypassing your ++maxlength[wl].

I don't think state serves any function at all though. You could dispose of it and just ignore anything with wordlengths of 0, for much simpler logic.
The Following User Says Thank You to Corona688 For This Useful Post:
Riker1204 (10-12-2016)
Sponsored Links
    #5  
Old Unix and Linux 10-12-2016
disedorgue disedorgue is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 19 January 2017, 6:55 PM EST
Location: France
Posts: 439
Thanks: 36
Thanked 166 Times in 154 Posts
You have another error: closure of first [C]if[/C] is after last [C]else[/C] and not before:
Your code corrected:

Code:
#include <stdio.h>

main() {

     int c, i, state, wl;
     int maxlength[15];

     #define IN 1
     #define OUT 0

     wl = i = c = 0;
     state = OUT;
    
     for (i=0; i < 15; ++i)
          maxlength[i] = 0;

     while ((c = getchar()) != EOF) {
          if (c == ' ' || c == '\t' || c == '\n') {
               if (state == OUT)
                    ;
               else if (state == IN) {
                    state == OUT;
                    ++maxlength[wl];
                    wl = 0;
               }
          }
          else { 
               ++wl;
               state = IN;
          }
     }
     printf("Number of 3 letter words: %d\n", maxlength[3]); 
}

EDIT: Script to count word sort by lenght:

Code:
sed -e 's/[\t ]/\n/g' file | awk 'length($0) {A[length($0)]++}END{for (i in A) print "word length of "i"=>"A[i]}'

Regards.

Last edited by disedorgue; 10-12-2016 at 07:50 PM.. Reason: Add script to count word by lenght
The Following User Says Thank You to disedorgue For This Useful Post:
Riker1204 (10-12-2016)
Sponsored Links
    #6  
Old Unix and Linux 10-12-2016
Riker1204 Riker1204 is offline
Registered User
 
Join Date: May 2012
Last Activity: 14 October 2016, 4:40 PM EDT
Posts: 25
Thanks: 9
Thanked 0 Times in 0 Posts
That was it! Its working now. Thanks to both of you for the help.
Sponsored Links
    #7  
Old Unix and Linux 10-13-2016
flimbar flimbar is offline
Registered User
 
Join Date: Aug 2016
Last Activity: 13 October 2016, 10:47 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Looks like an assignment question to me
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
if, word count azertyazerty Shell Programming and Scripting 3 12-20-2010 10:18 AM
Word count aadi_uni UNIX for Advanced & Expert Users 4 12-03-2009 11:07 AM
Word count of lines ending with certain word warlock129 Shell Programming and Scripting 8 08-30-2009 04:37 AM
word count wc chaandana Fedora 5 05-05-2009 11:47 AM
specified word count bhaviknp Shell Programming and Scripting 5 02-27-2008 11:23 AM



All times are GMT -4. The time now is 05:51 AM.