4.16 LAB: Checker for integer string
Instructor note:
This zyLab counts as a homework grade and is due by 11:59pm on 9Oct.
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. You may assume that the string does not contain spaces and will always contain less than 50 characters.
Ex: If the input is:
1995
the output is:
yes
Ex: If the input is:
42,000
or
1995!
the output is:
no
Hint: Use a loop and the isdigit() function (don't forget to include the ctype library).
c progamming
#include<stdio.h> int main(void) { char userString[20]; int isValid = 1, i; scanf("%s",userString); for(i = 0;userString[i]!='\0';i++){ if(!(userString[i]>='0' && userString[i]<='9')){ isValid = 0; break; } } if(isValid==1) { printf("yes\n"); } else{ printf("no\n"); } return 0; }
Get Answers For Free
Most questions answered within 1 hours.