Question

4.16 LAB: Checker for integer string Instructor note: This zyLab counts as a homework grade and...

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

Homework Answers

Answer #1
#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...