Stranger
Stranger

Reputation: 125

C: Not counting spaces and new lines

I have the following source code to count blank spaces, new lines and characters in a file:

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(){
    int fd;
    int b=0, nl=0, c=0;
    char ch[1];
    fd=open("text.txt", O_RDONLY);

    while((read(fd, ch, 1))>0)
    {
        if(ch==" ")
            b++;
        if(ch=="\n")
            nl++;
        c++;
    }
    printf("no of blanks %d\n", b);
    printf("no of new line %d\n", nl);
    printf("no of characters %d\n", c);
}

The result is like this:

no of blanks 0
no of new line 0
no of characters 24

My text.txt file's content is:

hello world
hello
world

Number of charactes is right (it includes spaces and new lines). But why are the result of variables b and nl wrong?
PS: I'm new to C, but have a little practice in C++.

Upvotes: 1

Views: 123

Answers (5)

Ajay Sivan
Ajay Sivan

Reputation: 2989

#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h> // or just include <string> it may vary depending on the compiler you use
int main(){
int fd;
int b=0, nl=0, c=0;
char ch[1];
fd=open("text.txt", O_RDONLY);

while((read(fd, ch, 1))>0)
{
if(strcasecmp(ch, " ") == 0) //you need to use strcasecmp() instead of == for strings
b++;
if(ch[0] == '\n') //you can also check like this.
nl++;
c++;
}
printf("no of blanks %d\n", b);
printf("no of new line %d\n", nl);
printf("no of characters %d\n", c);
}

Upvotes: 1

Shivind
Shivind

Reputation: 57

In C language,you can't directly compare two strings. You have to use strcmp(char *str1,char *str2) or strncmp(char *str1,char *str2,ssize_t size).

If you will directly compare the strings it will return 0,which is why blanks and newlines are not incrementing.

Try this correction.

#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
 int fdesc;
 int blanks=0, newlines=0, characters=0;
 char buf[1];
 fdesc=open("text.txt", O_RDONLY);
 while((read(fdesc,buf, 1))>0)
 {
  if(strcmp(buf," "))
   blanks++;
  if(strcmp(buf,"\n"))
   newlines++;
  characters++;
 }
 printf("no of blanks %d\n", blanks);
 printf("no of new line %d\n", newlines);
 printf("no of characters %d\n", characters);
}

Upvotes: 0

Stranger
Stranger

Reputation: 125

Thanks to all ur feedback I managed to fix the code and finally it looks like that :

#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(){
int fd;
int b=0, nl=0, c=0;

char ch[1];
fd=open("text.txt", O_RDONLY);

while((read(fd, ch, 1))>0)
{
if(ch[0]==' ')
b++;
if(ch[0]=='\n')
nl++;
c++;
}
printf("no of blanks %d\n", b);
printf("no of new line %d\n", nl);
printf("no of characters %d\n", c);
}

Upvotes: 0

tdao
tdao

Reputation: 17668

if(ch==" ")

should be

if(ch==' ')

And same for the other comparison, "\n" should be '\n'

The double quotation is for string. Use single quotation for character.

And yes you should use fopen instead of low level open call.

int ch;
FILE *fp=fopen("text.txt", "r");

while((ch = getc(fp)) != EOF)
{
    if(ch==' ')
        b++;
    if(ch=='\n')
        nl++;
    c++;
}

That should solve the issue.

Upvotes: 2

babon
babon

Reputation: 3774

Try putting the space " " and newline character "\n" in single quotes and declare ch as char.

Upvotes: 0

Related Questions