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]);
        }
}

Recommended Answers

All 2 Replies

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.