按如下函数原型编程实现求一个字符串的长度,并检查其是否全由数字组成。 求字符串长度的函数原型: int Length(char p[]); 检查字符串是否全由数字组成的函数原型: int IsAllDigit(char p[]);/*若全由数字组成,则函数返回1,否则返回0*/ **输入提示信息:"Please input a string:" **输入格式:"%s",或者用gets() **输出提示信息和格式: "The string has %d characters.\n" "The string is digit string." "The string is not digit string." 注:不能使用指针、结构体、共用体、文件、goto、枚举类型进行编程。
时间: 2024-02-15 21:01:43 浏览: 56
好的,这里是代码实现:
```
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int Length(char p[]) {
int len = 0;
while (p[len] != '\0') {
len++;
}
return len;
}
int IsAllDigit(char p[]) {
int len = Length(p);
for (int i = 0; i < len; i++) {
if (p[i] < '0' || p[i] > '9') {
return 0;
}
}
return 1;
}
int main() {
char str[MAXLEN];
printf("Please input a string: ");
scanf("%s", str);
int len = Length(str);
printf("The string has %d characters.\n", len);
if (IsAllDigit(str)) {
printf("The string is digit string.\n");
} else {
printf("The string is not digit string.\n");
}
return 0;
}
```
这里使用了while循环计算字符串长度,使用了scanf函数读入字符串,使用了宏定义定义了字符串长度的最大值。
阅读全文
相关推荐
















