Testing characters in a string: is digit and is alpha : String Compare « String « C / ANSI-C






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);
}


           
       








Related examples in the same category

1.Compare strings: strcmp
2.Our own string compare function
3.String compare: how to use strncmp
4. Compare two strings: how to use strcmp
5. Compare some characters of two strings: how to use strncmp