gpt4 book ai didi

检查回文

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:43 33 4
gpt4 key购买 nike

下面的代码应该检查最多九个字符的回文,尽管我将数组设置为 10 以便为 null 留出空间。问题是当命令屏幕出现以输入信息时,每次都会卡住。我不确定问题出在哪里,也确定代码中存在我未能注意到的错误。感谢您的帮助。

#include <stdio.h>
#include <stdlib.h>
/*Delare the prototypes*/
char palindromes(char[], int, int);
char backwards(char[], int, int);

int main ()
{
const int arraysize= 10;/*Sets a size for the array*/
char userinput[10];/*Declares the character array for the input*/
int palindex= 0;/*Declares the index to check for a palindrome*/
int index= 0;/*Index for the counting loop*/
int counter= 0;/*Counts number of elements entered in the array*/
int palcheck= 1;/*Sets a value for the palindrome checking loop, for verification*/
int charswap= 0;/*Sets an index for the loop that swaps the values*/
int printindex= 0;/*Index to print the values on the screen*/

printf("Please enter a series of nine or less characters to test for a palindrome.\n");
scanf(" %9s", &userinput);
printf("\n");

for(index; userinput[index] !=0; index++)
{
counter +=1;
}/*End of array element counter*/


for(palindex; palindex < (counter/2); palindex++)
{
palcheck= palindromes(userinput, counter, palindex);
if(palcheck = 0)
break;
}/*End of palidrome verification loop*/

if(palcheck = 0)
{
printf("Your input was not a palindrome \n");
}/*End of if statement*/
else
{
printf("Your input was a palindrome \n");
}/*End of else statement*/

for(palindex; palindex < (counter/2); palindex++)
{
backwards(userinput, counter, palindex);
}/*End of reversing the array loop*/

printf("Your input backwards is: ");
for(printindex; printindex < counter; printindex++)
{
printf("%c", userinput[printindex]);
}/*End of printing backwards loop*/
}/*End of main function*/

char palindromes(char userinput[], int counter, int palindex)
{
char firstchar;/*Temp variable for the two items to be tested*/
char lastchar;/*Temp variable for the two iteams to be tested*/
firstchar = userinput[palindex];
lastchar = userinput[counter - palindex];
if(firstchar = lastchar)
{
return 1;
}
else
{
return 0;
}
}/*End of palidrome function*/

char backwards(char userinput[], int counter, int palindex)
{
char temp;/*Sets a temporary value to swap the two values*/
temp = userinput[palindex];
userinput[palindex] = userinput[counter-palindex];
userinput[counter-palindex] = temp;
}/*End of reverse function*/

最佳答案

错误的地方太多了,记不住,就一行一行地记下来,但这里有一些开始

  • 在所有覆盖变量的 if 语句中,使用 == 而不是 =
  • 你的计数器什么都不做,使用带有 %n 的 scanf 来查找读取的字符数
  • 你不想将计数器变量作为中间参数传递给你的回文函数,因为你的输入字符串中有尾随的 '\0'...而是使用 counter-1

可能还有其他人,但这是您原始代码的最低限度工作版本:

#include <stdio.h>
#include <stdlib.h>
/*Delare the prototypes*/
char palindromes(char[], int, int);
char backwards(char[], int, int);

int main ()
{
char userinput[10];/*Declares the character array for the input*/
int palindex= 0;/*Declares the index to check for a palindrome*/
int index= 0;/*Index for the counting loop*/
int counter= 0;/*Counts number of elements entered in the array*/
int palcheck= 1;/*Sets a value for the palindrome checking loop, for verification*/
int charswap= 0;/*Sets an index for the loop that swaps the values*/
int printindex= 0;/*Index to print the values on the screen*/

printf("Please enter a series of nine or less characters to test for a palindrome.\n");
scanf(" %9s%n", &userinput, &counter);
printf("\n");
for(palindex; palindex < (counter/2); palindex++)
{
palcheck = palindromes(userinput, counter-1, palindex);
if(palcheck == 0)
break;
}/*End of palidrome verification loop*/

if(palcheck == 0)
{
printf("Your input was not a palindrome \n");
}/*End of if statement*/
else
{
printf("Your input was a palindrome \n");
}/*End of else statement*/

for(palindex; palindex < (counter/2); palindex++)
{
backwards(userinput, counter, palindex);
}/*End of reversing the array loop*/

printf("Your input backwards is: ");
for(printindex; printindex < counter; printindex++)
{
printf("%c", userinput[printindex]);
}/*End of printing backwards loop*/
printf("\n");
}/*End of main function*/

char palindromes(char userinput[], int counter, int palindex)
{
char firstchar;/*Temp variable for the two items to be tested*/
char lastchar;/*Temp variable for the two iteams to be tested*/
firstchar = userinput[palindex];
lastchar = userinput[counter - palindex];
if(firstchar == lastchar)
{
return 1;
}
else
{
return 0;
}
}/*End of palidrome function*/

char backwards(char userinput[], int counter, int palindex)
{
char temp;/*Sets a temporary value to swap the two values*/
temp = userinput[palindex];
userinput[palindex] = userinput[counter-palindex];
userinput[counter-palindex] = temp;
}/*End of reverse function*/

关于检查回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22927276/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com