纯数字字符串检验

 

题目内容:

按给定函数原型编程检查一个字符串是否全由数字组成。

int IsAllDigit(char p[]);/*若全由数字组成,则函数返回1,否则返回0*/

在主函数中,从键盘输入一个字符串(假设字符串的最大长度为20个字符),调用函数IsAllDigit(),检查该字符串是否全由数字组成,然后在主函数中根据函数IsAllDigit()的返回值输出相应的提示信息。

程序运行结果示例1:

Please input a string:

help456↙

The string is not digit string.

程序运行结果示例2:

Please input a string:

20150216↙

The string is digit string.

字符串输入提示信息:"Please input a string:\n"

输入格式:  字符串输入采用 gets()函数

输出格式:

判断是纯数字字符串:"The string is digit string."

判断不是纯数字字符串:"The string is not digit string."

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

#include <stdio.h>

#include <string.h>

int IsAllDigit(char a[])

{

int i, len;

len = strlen(a);

for (i=0; i<len; i++)

{

if (a[i]>=58 || a[i]<=47)

return 0;

}

}

int main()

{

char a[20];

int flag=0;

printf ("Please input a string:\n");

gets (a);

if (IsAllDigit(a) == 0)

printf ("The string is not digit string.");

else

printf ("The string is digit string.");

return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_40629792/article/details/79012645
今日推荐