User314159
User314159

Reputation: 8065

Determine if input is digit or string and then print it back

I just started learning c and I'm reading about inputs. Let's say I want a c program that recognizes if what you entered is a digit or string ( not just a single char) and then prints it. Somethihng like this:

   int input, is_digit;
   is_digit = scanf("%d", input);
   printf("Please enter a digit or a string and then a newline\n");
   if (is_digit)
     printf("You entered the digit: %d", &input);
    else
     printf("You entered the string: %s", &input);

This doesn't work, as expected, but I wrote it to give an idea of what I'm trying to accomplish.

How would you do this in C?

Upvotes: 0

Views: 959

Answers (5)

LPs
LPs

Reputation: 16213

First of all you must ask to the user before read an input to user, then:

printf("Please enter a digit or a string and then a newline\n");
is_digit = scanf("%d", &input);

Second, scanf wants a pointer to int, in case of %d format, then

scanf("%d", &input);

Third, printf, to print an int, wants the value, then:

printf("You entered the digit: %d\n", input);

Fourth, you cannot print a string (%s) passing an int variable, then your code is completely wrong.

A simple solution could be, using isdigit standard function, this

int main()
{
    char buffer[128];
    int i=0;
    printf("Please enter a digit or a string and then a newline\n");
    scanf("%s", buffer);

    // Checks until the end of string that all chars are digits
    while ((buffer[i] != '\0') && (isdigit(buffer[i]) != 0))
    {
        i++;
    }

    // If index of buffer doesn't point to the end of string, means that a non digit char was found
    if (buffer[i] != 0)
    {
         printf("You entered a string\n");
    }
    else
    {
         printf("You entered digits\n");
    }

    return 0;
}

Upvotes: 0

ezFreak
ezFreak

Reputation: 82

The input you receive from scanf are in ascii-format, see Ascii Table.

All digits (0-9) are between the ascii values: 48-57 (decimal).

What you should begin with is to input only one digit/character and print it out in integer format (%d). Compare it with the ascii table. Just to get a feeling for it.

When you have done that you should continue with your task. Considering you want to be able to interpret characters and digits I'd read in a string instead of a digit. See scanf, on how to read strings.

Declare a char with a predetermined size. And be sure not to overflow it with scanf.

Then the following should be considered:

  1. If there is atleast one character that's not a digit, you should consider it a string.

Simply, you create a while loop (or for-loop) that will break whenever a null-terminator has been found. This is the end of the string.

In the loop, you have an int that increments each round (starts at 0). This int is the index to read one character at a time. Keep reading the characters, whenever there is a ascii-value being not a digit (see the ascii-values above) you set a flag to indicate it's a string. This will you get u on the track.

Upvotes: 0

Šimon Tóth
Šimon Tóth

Reputation: 36433

You can use the combination of getchar() and ungetc() to look at the first character on stdin, determine whether it is a digit using isdigit() and then put it back into the stream and read it with the corresponding scanf.

Upvotes: 0

CIsForCookies
CIsForCookies

Reputation: 12817

First of all, when using scanf to scan in %d format, you will get one of the following:

  1. scan an int, and then no need to check
  2. infinite loop - because of wrong user input, and then no need to check either.

If you want to scan user input you should use some other functions like getchar.

There is a function isdigit() inside <ctype.h>library to help you iterate over a string and determine if a char represents an int or not here.

Example:

char* str = "abc1";
int counter=0;
while(counter < 4){
  if(isdigit(*(str+counter))){
    printf("digit")
  };
  counter++;
}

Upvotes: 0

Dayal rai
Dayal rai

Reputation: 6606

You need to take all your input as string first then parse this input to check if it is number or not. In case of failed condition you can feel sure that input was string -

Have a look on below demo code -

fgets(s, sizeof(s), stdin);
valid = TRUE;
for (i = 0; i < strlen(s); ++i)
{
    if (!isdigit(s[i]))
    {
        valid = FALSE;
        break;
    }
}

Upvotes: 1

Related Questions