Avoiding the use of gets()
, fgets()
accomplishes the same task, and it's safer.
#include <stdio.h>
#include <ctype.h>
int main(){
char c[100];
printf("Enter the string:\n");
fgets(c,100,stdin);
int i = 0;
while(c[i] != '\0'){
if(isdigit(c[i])){
printf("%c",c[i]);
}
i++;
}
}
Edit:
Without the rest of your code, a reasonable source of error is the comparison isdigit(*pc) == 1
, by the standard,
C11 §7.4.1
The functions in this subclause return nonzero (true) if and only if the value of the argument c conforms to that in the description of the function.
That is, int isdigit(int c);
. isdigit
could be returning non-zero values for when it is true, and it can not be printed due to the comparison of just 1
.