isdigit c++ string code example

Example 1: isdigit c++

str = "10"
  
//Evaluates to True
isdigit(str[0])

Example 2: check if character in string is digit c++

#include<stdio.h>
#include<ctype.h>

int main() {
   char val1 = 's';
   char val2 = '8';

   if(isdigit(val1))
   printf("The character is a digit\n");
   else
   printf("The character is not a digit\n");

   if(isdigit(val2))
   printf("The character is a digit\n");
   else
   printf("The character is not a digit");

   return 0;
}

Example 3: isdigit c++

int isdigit ( int c );
Checks whether c is a decimal digit character.
  
A value different from zero (i.e., true)
if indeed c is a decimal digit. Zero (i.e., false) otherwise.


/* isdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
  char str[]="1776ad";
  int year;
  if (isdigit(str[0]))
  {
    year = atoi (str);
    printf ("The year that followed %d was %d.\n",year,year+1);
  }
  return 0;
}  

/* Output */
The year that followed 1776 was 1777

Tags:

Cpp Example