0

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
4
Views
5 Years
Discussion Span
Last Post by sansai
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?

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.