如果我已经将大小设置为 20,如何找到用户输入的数字
strlen
只会给我20
以及如何将其放入else if
循环for
中
这是一个检查用户输入的 10 位数字是否有效的程序
#include<stdio.h>
int validateString(char *inStr);
int main()
{
char stringInput[20];
printf("Please enter a 10 digit phone number: ");
scanf("%s",stringInput);
if (validateString(stringInput)== 1)
{
printf("%s is a valid phone number.\n",stringInput);
}
else if (validateString(stringInput)== 0)
{
printf("%s is not a valid phone number.\n",stringInput);
}
int validateString(char *inStr)
{
int i;
for (i=0;i<length;i++)
{
if(!(inStr[i] == '0' ||
inStr[i] == '1' ||
inStr[i] == '2' ||
inStr[i] == '3' ||
inStr[i] == '4' ||
inStr[i] == '5' ||
inStr[i] == '6' ||
inStr[i] == '7' ||
inStr[i] == '8' ||
inStr[i] == '9' ||
inStr[i] == ' ' ))
{
return 0;
}
else if ( //the input string is longer than 10 or less than 10)
{
return 0;
}
else
{
return 1;
}
}
}