【发布时间】:2017-02-27 17:45:52
【问题描述】:
我正在学习 C,但我一直在尝试创建一个程序。基本上我正在测试一个字符串是否只包含字母字符a-z 或A-Z。
我做了什么:
- 定义了一个名为
strisalpha的函数来执行此操作 - 在我的“测试台”中调用该函数,要求用户输入一个字符串
gcc 编译器出了什么问题:
testBench1.c:21:28: warning: implicit declaration of function 'atoi' [-Wimplicit-function-declaration]
integerCharValue = atoi( string[loopPointer1] );
这是我对strisalpha的定义:
int strisalpha(char *string)
{
int stringLength = 0;
int loopPointer1 = 0;
int integerCharValue = 0;
int dummyArgument = 0;
/* Get length of string */
stringLength = strlen (string);
printf("\nString length is: %d", stringLength);
/* ASCII Codes In Decimal */
A (65Decimal) to Z(90Decimal) and
a (97Decimal) to z (122Decimal)
Set up a loop and query if ASCII alphabetic character
*/
for (loopPointer1 = 1; loopPointer1 > stringLength; loopPointer1++ )
{
/* Convert character to integer */
integerCharValue = atoi( string[loopPointer1] );
printf ("%d \n", integerCharValue);
if (integerCharValue >= 65)
if (integerCharValue <= 90)
return 1; /* Upper case alphabetic character, so OK */
else if (integerCharValue >= 97)
if (integerCharValue <= 122)
return 1; /* Lower case alphabetic character, so OK */
else
结果总是显示我输入了一个 ASCII 字符,即使我没有输入。请有人能说明我做错了什么吗?谢谢
【问题讨论】:
-
1)
loopPointer1 = 1; loopPointer1 > stringLength;-->loopPointer1 = 0; loopPointer1 < stringLength;2)integerCharValue = atoi( string[loopPointer1] );-->integerCharValue = string[loopPointer1]; -
什么是“字母数字”??
-
“gcc 编译器出了什么问题” - 不是编译器编写了强烈反对的代码。
-
感谢 Olaf 澄清这一点。
标签: c type-conversion ascii