How to check if a string contain only one digit in C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>

int contain_multiple_digit(char* s) {
    int count = 0;;

    while (*s) {
        if (isdigit(*s)) {
            count++;
        }
        s++;
    }

    return count == 1;
}

int main(void)
{
    char str[] = "C 7 is a general purpose computer programming language";

    if (contain_multiple_digit(str)) {
        puts("yes");
    }
    else {
        puts("no");
    }

    return 0;
}




/*
run:

yes

*/

 



answered Dec 11, 2023 by avibootz
edited Dec 11, 2023 by avibootz
...