You are confusing reading a number with reading characters. When you read with fgets
(or any other function) you read characters. The characters you read are ASCII characters that are represented by numeric values 0-127
(for the most part, there is wide-char support). See ASCII Table The first 31-chars (ASCII values 0-30
) are your control and whitespace characters (e.g. nul-character
, tab
, newline
, ...) 127
(del) is likewise a non-printing character. The remainder 31-126
make up the printable characters (including the printable characters 0-9
)
(note: you never use gets
which is so prone to exploit and buffer overrun it has been removed from the C11 library)
Do not confuse or equate the number 0
with the ASCII character '0'
(note the single-quotes).
In your code when you attempt to print each character, e.g.
printf("%d \n", *labelPtr);
You are printing the ASCII value (the decimal value) for the ASCII character. You are looking for '0'
and '1'
and getting 48
and 49
. That is because the ASCII code for character '0'
is 48
and for '1'
is 49
. If you wish to print the character, you would need:
printf("%c\n", *labelPtr);
Further, your counts will never register anything. Why? You are testing for 0
and 1
instead of '0'
and '1'
. (and noting that 0
is the nul-character -- so you would be beyond the last character in the string before it ever tested true). Instead you need:
if (*labelPtr == '0')
zeroCount++;
if(*labelPtr == '1')
oneCount++;
Putting it altogether and cleaning up unneeded/unused variables, you could do something like the following:
#include <stdio.h> /* using printf, gets, BUFSIZ */
int main (void)
{
char input[BUFSIZ] = "",
*labelPtr = input;
int n = 0,
zeroCount=0,
oneCount=0;
fgets (input, BUFSIZ, stdin);
for (; *labelPtr; labelPtr++, n++)
{
printf ("read ASCII char '%c' (%3d - decimal)\n",
*labelPtr, *labelPtr);
if (*labelPtr == '0')
zeroCount++;
if(*labelPtr == '1')
oneCount++;
}
printf("characters: %d\nzeros : %d \nones : %d \n",
n, zeroCount, oneCount);
}
Example Use/Output
$ echo "my 10 dogs have 100 fleas" | ./bin/zeroonect
read ASCII char 'm' (109 - decimal)
read ASCII char 'y' (121 - decimal)
read ASCII char ' ' ( 32 - decimal)
read ASCII char '1' ( 49 - decimal)
read ASCII char '0' ( 48 - decimal)
read ASCII char ' ' ( 32 - decimal)
read ASCII char 'd' (100 - decimal)
read ASCII char 'o' (111 - decimal)
read ASCII char 'g' (103 - decimal)
read ASCII char 's' (115 - decimal)
read ASCII char ' ' ( 32 - decimal)
read ASCII char 'h' (104 - decimal)
read ASCII char 'a' ( 97 - decimal)
read ASCII char 'v' (118 - decimal)
read ASCII char 'e' (101 - decimal)
read ASCII char ' ' ( 32 - decimal)
read ASCII char '1' ( 49 - decimal)
read ASCII char '0' ( 48 - decimal)
read ASCII char '0' ( 48 - decimal)
read ASCII char ' ' ( 32 - decimal)
read ASCII char 'f' (102 - decimal)
read ASCII char 'l' (108 - decimal)
read ASCII char 'e' (101 - decimal)
read ASCII char 'a' ( 97 - decimal)
read ASCII char 's' (115 - decimal)
read ASCII char '
' ( 10 - decimal)
characters: 26
zeros : 3
ones : 2
(note: the ' and ' on the next line with ( 10 - decimal)
is the '\n'
character)
Look things over and let me know if you have further questions.