Check if a string has only numbers in C?

前端 未结 5 979
予麋鹿
予麋鹿 2021-01-24 17:57

I\'m trying to write a simple code to check if a string only has numbers in it. So far it\'s not working, any help would be appreciated.

#include          


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 18:39

    #include 
    #include 
    
    int main()
    {
        char numbers[10];
        int i, correctNum = 0;
    
        scanf("%s", numbers);
    
        for(i = 0 ; i < 10 ; i++)
        {
            if(numbers[i]<48||numbers[i]>57)
            {
                correctNum = 1;
                break;
            }
        }
    
        if(correctNum == 1)
        {
            printf("That number has a char in it. FIX IT.\n");
        }
        else
        {
            printf("All numbers. Good.\n");
        }
        return 0;
    }
    

提交回复
热议问题