Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

printing words lemgthwise in c

#include <stdio.h>

#define SIZEOFWORDSOFLENGTH 21

int main() {
    int wordsOfLength[SIZEOFWORDSOFLENGTH];
    int c, i, j;
    int lengthCounter = 0;

    /*Initializing all array elements to 0.*/
    for (i = 0; i < SIZEOFWORDSOFLENGTH; i++)
        wordsOfLength[i] = 0;

    /*Going through the input and counting.*/
    while ((c = getchar()) != EOF) {
        ++lengthCounter;
        if (c == ' ' || c == '	' || c == '
') {
            ++wordsOfLength[lengthCounter - 1];
            lengthCounter = 0;
        }
    }

    for (i = 0; i < SIZZEOFWORDSOFLENGTH; i++) {
        printf("Words of Length %d: ", i);
        /*The third argument of the following for loop was previously j = j*/
        for (j = 0; j < SIZEOFWORDSOFLENGTH; j++) {
            while (j < wordsOfLength[i]) {
                printf("|");
                /*Was previously j++ instead of break*/
                break;
            }
        }
        printf("
");
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #printing #words #lemgthwise
ADD COMMENT
Topic
Name
4+3 =