We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,654 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

The c programing language Exercise 1-13

Hello was wondering if anyone could help me with this code. I am writing it for exercise 1-13 in the book "The C Programing Language". I am tring to get an array of word lengths. When I compile it i get the warning multi-character character constant. Also the code doesn't work. Any ideas?

thanks

#include <stdio.h>
#define IN 1
#define OUT 0



main()
{
        int c, nc, i, state;
        int ndigit[26];

        state = OUT;
        nc = 0;

        for(i = 0; i < 26; ++i)
                ndigit[i] = 0;

        while ((c = getchar()) !='\t') {
                if (c == ' ' || c == '|n' || c == '\t'){
                        state = OUT;
                        ++ndigit[nc-'0'];
                        nc = 0;
                }
                else if (state == OUT) {
   state = IN;
                        ++nc;
                }
                else if (state == IN) {
                        ++nc;
                }
        for (i = 0; i < 26; ++i)
                printf("%d", ndigit[i]);
        }
}
2
Contributors
2
Replies
1 Day
Discussion Span
9 Months Ago
Last Updated
3
Views
sansai
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

there's a trailing "|" at the 2nd condition of your while loop at line 19
My guess is you'd want to use backslash instead

I am tring to get an array of word lengths.

You need to add more details, do you mean the length of every word in a string will be stored in an array?

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 13

Ok thanks that helped fix the warning. So what I was hopeing to get as an output was an array of numbers. If my input was 2, 3 letter words and 3, 5 letter words my output would be something like

0 0 0 2 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

However all i am getting is a bunch of 0's and way more than 26

sansai
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0598 seconds using 2.71MB