-3

Given task is:

Enter 10 characters. For each character entered the corresponding function prints whether it is a digit 0-9 or not.(Also I use older compiler if anyone concerns about my "gets()" and goal is to do this without pointers.)

So far I tried something like this, but for some reason it does not work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

    char character[10][1];
    char zero ='0';
    char nine ='9';
    int i;

    printf("Enter 10 characters:\n");

    for(i=0;i<10;i++){
        gets(character[i]);
    }

    for(i=0;i<10;i++){
        if(strcmp(character[i],zero)>=0 && strcmp(character[i],nine)<=0){
            printf("%d. character '%c' is a digit.", i, character[i]);
        }
        else{
            printf("%d. character '%c' is not a digit.", i, character[i]);
        }
        putchar('\n');
    }


    return 0;

} 

Also I tried this, but it can not output correctly characters:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

    char character[10][1], pom[10][1];
    int num_character[10];
    int i;

    printf("Enter 10 characters:\n");

    for(i=0;i<10;i++){
        gets(character[i]);
        strcpy(pom[i],character[i]);
        num_character[i]=atoi(character[i]);
    }

    for(i=0;i<10;i++){
        if(num_character[i]!=0){
            printf("Character '%c' is digit.", pom[i]);
        }
        else{
            printf("Character '%c' is not digit.", pom[i]);
        }
        putchar('\n');
    }

    return 0;

}

isdigit() also does not work after I include ctype header.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    `#include ` and `isdigit()`. – EOF Aug 31 '19 at 15:49
  • 1
    Never use obsolete `gets`, use `fgets`. You have the buffer overrun in your code. More info https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – 273K Aug 31 '19 at 16:29
  • You should have gotten an error or warning message on the incorrect call to `strcmp` in your first program. In both programs, you're calling `gets` (**never use `gets`**) with an array of length 1, which pretty much guarantees a buffer overflow. `isdigit` is part of a correct solution. How doesn't it work? – Keith Thompson Aug 31 '19 at 18:21
  • Possible duplicate of [Check if a char is a digit? (in C)](https://stackoverflow.com/questions/29321095/check-if-a-char-is-a-digit-in-c) – dandan78 Aug 31 '19 at 20:02

1 Answers1

0

You are doing several things wrong here, gets is never recommended and fgets will put new line character in the end.

The syntax for strcmp is:

int strcmp(const char *s1, const char *s2);

You need two strings as input in strcmp but you are using a string and a character.

Here, it is better to use a character array than a 2D array.

   #include <stdio.h>

    int main(void)
    {

            char character[10];       //a simple character array
            char zero ='0';
            char nine ='9';
            int i;

            printf("Enter 10 characters:\n");

            for(i=0;i<10;i++){
                    scanf(" %c", &character[i]);     //scan characters
            }
            for(i=0;i<10;i++)
            {
                    if(character[i] >= zero && character[i] <= nine)      //print
                            printf("%d. %c is a digit \n", i+1, character[i]);    
                    else
                            printf("%d. %c is not a digit \n", i+1, character[i]);
            }
            }
arvind
  • 275
  • 2
  • 11
  • It works, thanks. I just fail to understand why is it important to make space in scanf( %c", character[i]); ? When i run it with no space between ' " ' and ' % ' it won't work. – photosynthesis_bro Sep 02 '19 at 15:01
  • space before format specifier makes it so that scanf ignores all whitespaces like new line or space, when you don't put space, the `enter` you pressed previously is saved in the character array instead of the what you wanted to save – arvind Sep 02 '19 at 15:29