compiler errors - Program not working as well - C -
the following code gives me 0 value 'count' time...
#include <stdio.h> #include <stdlib.h> #include <string.h> #define size 128 int main () { char mychar , string [size]; int i; int count =0 ; printf ("please enter string: \n\n"); fgets (string, size, stdin); printf ("please enter char find: "); mychar = getchar(); (i=0 ; (string[i] == '\0') ; i++ ) if ( string[i] == mychar ) count++; printf ("the char %c appears %d times" ,mychar ,count); return 0; } thanks !
replace
int const count = 0; with
int count = 0; your trying change variable (count++) declared const which, obviously, not allowed.
edit: answer updated question should change loop condition string[i] == '\0' string[i] != '\0'. because loop runs while condition true. string[i] != '\0' true whole string except terminating null byte while opposite true string[i] == '\0'. therefore, original loop didn't run single time.
Comments
Post a Comment