Myno
Myno

Reputation: 158

Check if stdin has a white spaces


How to check if in stdin it has a white spaces ? In this example I want to count all white spaces in av[1]. But I don't know how to represent white spaces begin with a '\' in av[1].

#include <stdio.h>

int main(int ac, char **av)
{
    int count;

    count = 0;
    if (ac == 2)
    {
        while (*av[1])
        {
            if (*av[1] == ' '|| *av[1] == '\t'||*av[1] == '\r'|| *av[1] == '\v'||*av[1] == '\n'||*av[1] == '\f')
            {
                count++;
            }
            av[1]++;
        }
        printf("%d\n",count);
    }
    else
        printf("Error\n");

    return(0);
}

Intput

./a.out "\n"

Output

0

Upvotes: 1

Views: 201

Answers (1)

klutt
klutt

Reputation: 31296

Your problem is that the shell does some magic before passing the arguments. If you send the arguments quoted it will work:

$ ./a.out "this string has four spaces"
4

At least it will work for spaces. How whitespaces are escaped is shell dependent.

Upvotes: 1

Related Questions