Amin Abourjaily
Amin Abourjaily

Reputation: 11

Program counting numbers of characters, lines and words in C

I'm new to C and I'm trying to write a code that counts the numbers of characters, lines and words in C. I don't know where to continue, If someone could please guide me the answer, that would be very appreciative. Thank you. I haven't learnt anything about strings yet, so i'm trying to do this without using string.

So far I have this

int countch =0;
int countwd =0;
int lines = 0;
char c;

printf("Enter text:");
while((c = getchar()) != EOF) {

if(c == ' ')
countwd++;
else if (countch++);
else if(c == '\n') {
    lines++;

putchar(c);
}


printf("The amount of characters is %d\n The amount of words is %d\n The amount of lines is %d", countch, countwd,lines );

Upvotes: 0

Views: 3036

Answers (4)

user7783026
user7783026

Reputation:

Here is what I came up with. Unfortunately, the program takes too long to compile for the auto-grader. Here is what my teacher said, "program might be waiting for input from a source that is different from the requirements, it keeps waiting until the auto grader kills your running process because it took too long. So make sure you test your program using the same input requirements specified for the task." Any advice on fixing it is appreciated.

//printf( "%lu %lu %lu\n", charcount, wordcount, linecount );
/*Write a C program called count.c that counts the number of characters, words and lines read from standard input until EOF is reached.
Assume the input is ASCII text of any length.
Every byte read from stdin counts as a character except EOF.
Words are defined as contiguous sequences of letters(a through z, A through Z) and the apostrophe(', value 39 decimal) 
separated by any character outside these ranges.
Lines are defined as contiguous sequences of characters separated by newline characters('\n').
Characters beyond the final newline character will not be included in the line count.
On reaching EOF, use this output command :
printf("%lu %lu %lu\n", charcount, wordcount, linecount);
Where charcount, wordcount and linecount are all of type unsigned long int.You may need these large types to handle long documents.*/
#include<stdio.h>
#include<string.h>

int main(void)
{
    unsigned long int charcount = 0, wordcount = 0, linecount = 0;
    int c;//getchar converts to int so use this

    while ((c!= EOF)//count every byte except EOF for characters
    {
        c = getchar();
        charcount++;
    }

    char word[1000] = { 0 };//buffer; never seen a word longer than this

    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        char word[1000] = { 0 };//buffer initialized to zero
        if ((getchar() >= 'A') && (getchar() <= 'Z') || (getchar() >= 'a' && getchar() <= 'z') || ((getchar() == '\'')))//any of these get put into array
            word[i] = getchar();
        else if ((getchar() != ' ') || (getchar() != '\n'))
        {
            wordcount++;
            memset(word, 0, sizeof(word));//clear the buffer
            i = 0;
            continue;
        }
        else if (getchar() == EOF)
        {
            wordcount++;
            break;
        }
        else
        {
            wordcount++;
            memset(word, 0, sizeof(word));
            i = 0;
            continue;
        }
    }


    char line[50000] = { 0 };
    for (i = 0; i < 50000; i++)
    {
        line[i] = getchar();
        if (getchar() == '\n')
        {
            linecount++;
            memset(line, 0, sizeof(line));
            i = 0;
            continue;
        }
        else if (getchar() == EOF)
        {
            linecount++;
            break;
        }
        else
        {
            linecount++;
            memset(line, 0, sizeof(line));
            i = 0;
            continue;
        }
    }
    printf("%lu %lu %lu\n", charcount, wordcount, linecount);


    return 0;
}

Upvotes: 0

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33283

Hera are a couple of observations:

  • c needs to be an int. Reason: EOF is outside the range of char, so currently, a truncated value will be compared to EOF and the comparison will fail.

  • countch should be increased always, don't place it in an if.

  • You should accept any whitespace character as word separator. #include <ctype.h> and use if (iswhite(c)) to find word boundaries.

  • Words can be separated by more than one whitespace. Also, the last word may not have any whitespace after it. Consider using a flag to keep track of whether the previous character was whitespace and only update the word count when the previous char was whitespace and the current char isn't whitespace.

Upvotes: 1

Dinesh balan
Dinesh balan

Reputation: 495

Try Like this

 Switch(c)
{
 case ' ':
    countwd++;
    break;
 case '\n':
    lines++;
    break;
 default:
    countch++;
}

Upvotes: 0

unwind
unwind

Reputation: 400129

This:

else if (countch++);

makes absolutely no sense.

You probably meant

if(c == ' ')
  countwd++;
else if(c == '\n')
  lines++;
else
  countch++;

That's at least closer to something sane, but consider what it will do if the input is "hello world", i.e. multiple adjacent spaces.

Upvotes: 1

Related Questions