gpt4 book ai didi

c - 在 C 中,我打印字符串中最大回文的代码没有打印整个回文字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:41 25 4
gpt4 key购买 nike

我正在做的作业是:找出字符串中最大的回文。回文是一个向后读取与向前读取相同的序列。赛车、前夕、皮划艇就是一些例子。我的问题是我的字符串没有打印出整个输出。我在这方面还很陌生,所以我知道的不多,但我认为打印有问题。如果有人能帮助我,我将非常高兴。

#include <stdio.h>
#include <string.h>
int palindromelength(char *str, int i, int j);
char str[100];
int main()
{
int i,j,len,n;
printf("Enter a string ");
fgets(str,sizeof(str),stdin);//takes user input
str[strcspn(str, "\n")] = 0;
len=strlen(str);
palindromelength(str, 0, len-1);//function call
return 0;
}
int palindromelength(char *str, int i, int j)//compare
{
int len=strlen(str);
i=0,j=len-1;//i starts from first letter and j starts from the last letter
while(i<=j && j!=0)//edit:&& j!=0 because i=0->str[0] and j=0->str[0] is the same first letter
{
if(str[i]==str[j])
{
printf("%c%c\n",str[i],str[j]);//edit:added str[j] but it just prints a letter twice
printf("if: i=%d j=%d str[i]=%c str[j]=%c\n",i,j,str[i],str[j]);/*edit:new
printf to check the i and j values and the corresponding letters under if*/
i++;//increment i
j--;//decrement j
}
if(str[i]!=str[j])//if letters aren't same
{
printf("if: i=%d j=%d str[i]=%c str[j]=%c\n",i,j,str[i],str[j]);/*edit:new
printf to check the i and j values and the corresponding letters under the other if statement*/
i=0;//i_initial?
j--;//only decrement j
}
}
return 0;
}
output//edit to print the new printf statements
Enter a string abcbade //expected:abcba
length is 7
if: i=0 j=6 str[i]=a str[j]=e
if: i=0 j=5 str[i]=a str[j]=d
aa
if: i=0 j=4 str[i]=a str[j]=a
bb
if: i=1 j=3 str[i]=b str[j]=b
cc
if: i=2 j=2 str[i]=c str[j]=c
output 2:
Enter a string dabae //expected:aba
length is 5
if: i=0 j=4 str[i]=d str[j]=e
if: i=0 j=3 str[i]=d str[j]=a
if: i=0 j=2 str[i]=d str[j]=b
if: i=0 j=1 str[i]=d str[j]=a
output 3:
Enter a string abcbacdcbaab //expected:abcba
length is 12
if: i=0 j=11 str[i]=a str[j]=b
aa
if: i=0 j=10 str[i]=a str[j]=a
if: i=1 j=9 str[i]=b str[j]=a
if: i=0 j=8 str[i]=a str[j]=b
if: i=0 j=7 str[i]=a str[j]=c
if: i=0 j=6 str[i]=a str[j]=d
if: i=0 j=5 str[i]=a str[j]=c
aa
if: i=0 j=4 str[i]=a str[j]=a
bb
if: i=1 j=3 str[i]=b str[j]=b
cc
if: i=2 j=2 str[i]=c str[j]=c

最佳答案

它没有打印完整的回文,因为你已经把你的 printf 放在支票里了。

   if(str[i]==str[j])//if letters are same
{
printf("%c",str[i]);//print palindrome letter
/*is there a way I can store all the palindrome
characters in a string and print that string?*/
i++;//increment i
j--;//decrement j
}

如您所见,索引“i”和“j”是回文的一部分,但您只打印“i”。所以只有回文的前半部分会被打印出来。

除此之外,我认为您还需要处理许多其他情况,例如有多个回文,您需要选择最大的一个。例如:abcxyzyxdefabcdedcbaxyz

所以我的建议是,

1) 实现一个返回最大回文的函数 字符串的索引 i(如果存在)。

2) 在循环中调用这个函数 从索引 i=0 到原始字符串的长度 2。

3) 存储 当前最大的回文索引和长度。在每个循环中更新它。

4) 最后打印出最大的回文。

函数 palindromelength() 正在做我在步骤 #1 中提到的任何事情。但是您不应打印字母,而应从中返回最大回文的长度。

在 main() 中,循环调用 palindromelength() 并更新索引,如下所示。

for(i=0; i<(len-1); i++)
{
palinrdomeLen = palindromelength(str, i, len-1);
if(palinrdomeLen > largestPalindromeLen)
{
largestPalindromeLen = palinrdomeLen;
largestPalindromeIdx = i;
}
}

除了回文长度(),

1) 您需要更新函数以返回回文的长度。

2) 当 str[i]!=str[j] 时,回文搜索应该从索引首字母“i”开始。

if(str[i]!=str[j])//if letters aren't same
{
i = i_initial; //i_initial is the "i" value passed to palindromelength()
j--;//only decrement j
}

关于c - 在 C 中,我打印字符串中最大回文的代码没有打印整个回文字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55486920/

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