输入若干数字,统计输入的个数
输入若干数,统计输入的个数,谁写给我看看?或者讲讲思路也可以。要求: 输入的是数,没有非单个字符,比如我要输入 56 58只算两个数
程序代码:
root@~ #cat lt107.c
#include <stdio.h>
#include <stdbool.h>
int main (void) {
char text[81];
int countWords (char string[]);
printf ("Enter some stirng:");
gets(text);
printf ("numbers=%i\n",countWords(text));
return 0;
}
int countWords (char string[]) {
int i,wordCount=0;
bool lookingForword=true;
bool number (const char c);
for(i=0;string[i]!='\0';i++) {
if(number(string[i])) {
if(lookingForword) {
wordCount++;
lookingForword=false;
}
}
else{
lookingForword=true;
}
}
return wordCount;
}
bool number (const char c) {
if(c>='0' && c<='9') {
return true;
}else{
return false;
}
}
root@~ #
root@~ #./lt107
Enter some stirng:sdf wer 34 56 123
numbers=3
root@~ #./lt107
Enter some stirng:sdf sdf sdf
numbers=0
root@~ #./lt107
Enter some stirng:sdf 234 sdf 23
numbers=2
root@~ #
