简体   繁体   中英

counting the number of digits in using only scanf in c

I need to limit the input from a user to only positive values, and count the number of digits in that number. The user will only type in a (+/-) whole number up to 9 characters long.

I'm only allowed to use the scanf function and for, while, or do-while loops.(I saw in similar questions how to do this using getchar, but I can only use scanf). I'm not allowed to use arrays, or any other library besides stdio.h and math.h

I know that if I write:

n=scanf("%c%c%c%c%c",&a,&b,&c,&e,&f);

n will count the number of successful scanf conversions.

The problem i'm having is that when I define the input with char, it does everything I want except that the user MUST enter 5 characters. So if the user wants to input "55" he has to press "5" "5" "enter" "enter" "enter".

I need the program to move on after the first "enter" but also be flexible to receive a number up to 9 digits long.

again, I can't use getchar or anything fancy. Just the really basic stuff in C that you learn in the first 2 weeks.

Use scanf to read the number into a long int , then use a for loop with a /10 to count the number of digits

What do you want the program to do in case of a -ve number being entered?

#include<stdio.h>
int main()
{

    long int a;
    int b;
    do
    {
    scanf ("%ld",&a);
    if(a<0)
        printf ("invalid input");
    }while(a<0);

    for(b=0;a!=0;b++,a=a/10);
    printf("%d",b);
}

(does not handle -ve numbers specially)

Something like

#include <stdio.h>

int main(void)
{
  char buffer[10] = { 0 };
  size_t len;

  scanf("%9[0-9]", buffer);
  for(len = 0; buffer[len] != 0; len++) ;
  printf("%zu '%s'\n", len, buffer);

  return 0;
}

works, but I don't know if it fits your need.

EDIT (bits of explanation)

You can replace size_t with int (or unsigned int), though size_t is better. If you do, use %d or %u instead of %zu .

The basic idea is to exploit a feature of the format of scanf; the 9[0-9] says the input is a sequence of up to 9 char in the given set ie the digits from 0 to 9.

The for(...) is just a way to count char, a simple implementation of a strlen . Then we print the result.

The approach I would take would be the following.

  1. Loops are allowed, so go ahead and set one up.
  2. You need to have a variable somewhere that will keep track of what the current number is.
  3. Think about typing out a number, one character at a time. What needs to happen to the current_number variable?
  4. You need to stop the loop if a return key has been pressed.

Something like this should do for starters, but I'll leave the rest up to you, specifically what return_check(ch) , update_state(current_val) and char_to_int(ch) looks like. Also note that rather than use a function, feel free to put your own function directly into the code.

int current_val=0;
int num_digits=0;
char ch="\0"

for (num_digits=0;return_check(ch) && num_digits<=9;num_digits++)
{
  fscanf("%c");
  current_val=update_state(current_val);
  current_val=current_val+char_to_int(ch);
}

As for the logic in update_state(), think about what happens, one character at a time, if a user types in a number, like 123456789. How is current_val different from a 1 to a 12, and a 12 to a 123.

Can you wrap a loop around it, something like (I don't know if all of the syntax is right):

const int max_size=9
int n=0; //counter for number of chars entered
char a[max_size-1];
do {
    scanf(%c,&a[n]);
    n++;
} while (a[n] != '\r' && n<max_size)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM