amrit973
amrit973

Reputation: 19

Program to count lines words and characters in c

In this code I want to count number of characters newlines and words. But there is some problem in second while loop which I am not able to understand; also code is showing no output.

//program to count newlines new words and characters

#include<stdio.h>
int main()
{
    int c;
    int nl,nw,nc;//nl=newline,nw=new word,nc=new charcacter
    nl=nw=nc=0;
    while((c=getchar())!=EOF)
    {
        nc++;
        if(c=='\n')
        nl++;
        else if(c!=(' ')||c!=('\n'))
        {
            nw++;
            while(c!=' '||c!='\n')
            { 
                c=getchar();
                nc++;
            }
            nc++;
        }
    }
    printf("%d %d %d",nl,nc,nw);
}

Upvotes: 1

Views: 1844

Answers (3)

Jithu
Jithu

Reputation: 1

In fact the inner loop is unnecessary. You can read all characters using the outer loop. Then check whether the char is '\n', or ' ' and increase the counters accordingly.

//program to count newlines new words and characters
#include<stdio.h>
int main()
{
    int c;
    int nl,nw,nc;//nl=newline,nw=new word,nc=new charcacter
    nl=nw=nc=0;
    while((c=getchar())!=EOF)
    {
        nc++;
        if(c==' ')
        nw++;
        else if(c=='\n')
        {
            nw++;
            nl++;

    }
    printf("%d %d %d",nl,nc,nw);
} 

Upvotes: 0

EsmaeelE
EsmaeelE

Reputation: 2708

This is a try to solve similar problem.


Code

#include <stdio.h>

///check is given character is a valid space (' ','\n', '\t' ) or not
int checkSpace(char c){
    if(c=='\n'||c==' '||c=='\t'){
        return 1;
    }else{
        return 0;   
    }
}

///if given character is in ascii valid return true
int checkASCII(char c){
    if(c>32&&c<127){
        return 1;
    }else{
        return 0;   
    }
}

///a function that prints all contents of ascii file
void printFile(FILE *fp){
    char c;
    while((c=fgetc(fp))!=EOF){
        printf("%c", c);
    }
}

////main function 
int main(int argc, char **argv){

    FILE *fp=fopen(argv[1],"r");
    if(!fp){
        printf("error when opening file\n");    
    }

    char c=0;
    int numWord=0;
    int numChar=0;
    int flag=0;
    int visiblenumChar=0;
    int newLine=0;

    while((c=fgetc(fp))!=EOF){
        numChar++;
        if(c=='\n'){
            newLine++;
        }
        if(flag && checkSpace(c)){
            numWord++;
            flag=0;     
        }       
        if(checkASCII(c)){
            flag=1;//first ascii read   
            visiblenumChar++;   
        }   
    }

    //program output
    printf("file name: %s\n", argv[1]);
    printf("#All characters: %d\n", numChar);
    printf("#Visible characters: %d\n", visiblenumChar);
    printf("#Words: %d\n", numWord);
    printf("#New lines: %d\n", newLine);

    return 0;   
}

Compile

gcc counter.c -o counter

Run

./counter yourtextfile.txt

Upvotes: 0

dbush
dbush

Reputation: 225197

This condition will always be true:

(c!=(' ')||c!=('\n'))

The logical OR operator || evaluates to true if either side evaluates to true. If c is a space then the first part will be false but the second part will be true, making the result true. If c is a newline then the first part will be true and the second part won't even be evaluated, making the result true. If c is any other value, both parts will be true.

You want to use a logical AND here instead, which is only true if both parts are true. You want the condition to be true if c is not a space AND c is not a newline:

((c!=' ') && (c!='\n'))

Upvotes: 6

Related Questions