C中的编译器错误--应为';)';在';之前';代币
我正在编写一个基本程序来检查字符串是否是回文C中的编译器错误--应为';)';在';之前';代币,c,compiler-errors,while-loop,palindrome,C,Compiler Errors,While Loop,Palindrome,我正在编写一个基本程序来检查字符串是否是回文 #include <stdio.h> #include <string.h> //Has some very useful functions for strings. #include <ctype.h> //Can sort between alphanumeric, punctuation, etc. int main(void) { char a[10
#include <stdio.h>
#include <string.h> //Has some very useful functions for strings.
#include <ctype.h> //Can sort between alphanumeric, punctuation, etc.
int main(void)
{
char a[100];
char b[100]; //Two strings, each with 100 characters.
int firstchar;
int midchar;
int lastchar;
int length = 0;
int counter = 0;
printf(" Enter a phrase or word for palindrome checking: \n \n ");
while ((a[length] == getchar()) !10 ) //Scanning for input ends if the user presses enter.
{
if ((a[length -1]), isalpha) // If a character isalpha, keep it.
{
b[counter] = a[length-1];
counter++;
}
length--; //Decrement.
}
makelower(b, counter); //Calls the function that changes uppercase to lowercase.
for( firstchar = 0; firstchar < midchar; firstchar++ ) //Compares the first and last characters.
{
if ( a[firstchar] != a[lastchar] )
{
printf(", is not a palindrome. \n \n");
break;
}
lastchar--;
}
if( firstchar == midchar )
{
printf(", is a palindrome. \n \n");
}
return 0;
}
//Declaring additional function "makelower" to change everything remaining to lowercase chars.
int makelower (char c[100], int minicount)
{
int count = 0;
while (count <= minicount)
{
c[count] = tolower(c[count]);
}
return 0;
}
我上下查看了一下,但没有发现任何不合适的括号或不合适的括号。我唯一能想到的是我缺少了一个逗号或某种标点符号,但我试着在一些地方放一个逗号,但没有用
对不起,如果这太具体了。提前谢谢
while ((a[length] == getchar()) !10 )
看起来您正在尝试的是将getchar()
的结果分配给a[length]
,并验证该结果是否不等于10
。拼写如下:
while ((a[length] = getchar()) != 10)
=
是作业,=
是测试
此外,您的计数器很混乱length
初始化为0
,并且仅递减,这将导致在第一次递减后从数组的前端脱落。这是不可能发生的,因为您试图访问[length-1],这也会失败。在访问刚从getchar()读取的字符时,这看起来像一个,也称为
此外,由于没有检查记录输入的长度是否超过缓冲区的长度a[100]
,因此您也可能从缓冲区的末端跌落
回文检查功能的计数器也已关闭
midchar
和lastchar
从不初始化,midchar
从不设置,lastchar
在没有设置值的情况下递减。您最好测试a[firstchar]==a[(计数器-1)-firstchar]
(a[length]==getchar())!10你是说“!=10”吗?(不等于10)如果需要两个条件,则需要通过&&(and)或| |(or).lenght=0将它们连接起来,那么[lenght-1]是如何连接的呢?!这是命中注定的=10,表示与“回车”对应。但我相信输入/返回对于ANSI来说实际上是13。谢谢大家的输入。另外,getchar()返回一个int,并将其放入字符数组中。在文件末尾,这将把字符255放入字符串,并落入无限循环中。谢谢,我对getchar()的返回值不是很熟悉。
while ((a[length] = getchar()) != 10)