Mega Code Archive

 
Categories / C / Code Snippets
 

Testing characters in a string is digit and is alpha

#include <stdio.h> #include <ctype.h> void main() { char buffer[80]; int i = 0; int num_letters = 0; int num_digits = 0; printf("\n string with char and digits:\n"); gets(buffer); /* Read a string into buffer */ while(buffer[i] != '\0') { if(isalpha(buffer[i])) num_letters++; /* Increment letter count */ if(isdigit(buffer[i++])) num_digits++; /* Increment digit count */ } printf("\n The string contained %d letters and %d digits.\n", num_letters, num_digits); }