Advertisement
u53r

Contare cifre di un numero (ctype.h)

Jan 11th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. // Contare le cifre di un numero
  2. /*
  3.   ATTENZIONE:
  4.   * il programma ignorerร  le eventuali cifre presenti dopo il primo carattere non numerico,
  5.   * gli zero all'inizio verranno contati come cifre (es. il numero 0012 avrร  4 cifre)
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. int main() {
  12.   char s[100];
  13.   int i,c=0; // i = contatore for; c = contatore cifre
  14.   printf("Inserisci un numero --> ");
  15.   fgets(s,100,stdin);
  16.   for (i = 0;i < strlen(s); i++) { // Scorro tutta la stringa
  17.     if (isdigit(s[i])) { // Controllo che il carattere corrente sia numerico
  18.       c++; // Incremento il contatore
  19.     }else{
  20.       break; // Esco dal ciclo al primo carattere non numerico
  21.     }
  22.   }
  23.   printf("Cifre : %d\n", c);
  24.   return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement