gpt4 book ai didi

c - While 循环运行一次

转载 作者:行者123 更新时间:2023-11-30 19:45:23 25 4
gpt4 key购买 nike

我正在尝试运行一个程序,该程序将重复读取用户的一封信,最多输入 12 封信。如果用户输入他们输入的哨兵值,则循环应终止。但是,一旦在循环中读取第一个字符,循环就会终止。

此外,程序会将相同的单词以相反的顺序放入另一个数组中,然后检查它们以查看第一个数组(向前读取)是否与另一个数组(向后读取)相同。如果是,则显示该单词是回文。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
int charCount, counter, i, temp, check,check2;
char letter[12], letter2[12];
charCount = 0;
counter = 10;
check = 0;
i = 1;
check2 = 0;

printf("Enter your sentinel value.:");
scanf_s(" %c", &letter[check2]);


while ((i<13) && (letter[i] != letter[check2]))
{
printf("Enter individual letters in word (in order).:");
scanf_s(" %c", &letter[i]);

charCount++;

if (letter[i] == letter[check2])
{
break;
}
i++;

}


printf("Letters entered:%i\n", charCount);


for (i = 0; i < charCount; i++)
{
letter2[i] = letter[i];
}

for (i = 0; i <= (charCount / 2); i++)
{
temp = letter2[counter];
letter2[counter] = letter2[i];
letter2[i] = temp;
counter--;
}

for (i = 0; i <= charCount; i++)
{
if (letter[i] = letter2[i])
{
check++;
}
}

if (check = charCount)
{
printf("Word is a palindrome.\n");
}

system("PAUSE");
return 0;

}

最佳答案

第一次进入while循环时,字母[1]的值将被取消分配,对吗?我认为您可以将该条件从 while 循环中取出,因为您正在 while 循环内的 if 语句中考虑它

关于c - While 循环运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26519420/

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