您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在C程序中,将句子中最长的回文单词打印出来

2026/2/8 9:04:37发布21次查看
给定一个句子,挑战是从给定的句子中找到最长的回文
什么是回文?回文是一个单词或序列,即使在之后其含义仍然保持不变反转字符串
示例 - nitin,反转字符串后其含义保持不变。
挑战是从给定的句子中找到最长的回文。
喜欢的句子是:malayalam liemadameil iji
它包含三个回文词,但最长的是 - liemadameil
算法startstep 1 -> declare start variables i, j, k, l, max to 0, index to -1, check to 0, count to 0step 2 -> loop for i to 0 and i<strlen(str) and i++ set max =0, k =i and j=i+1 loop while str[j]!=' ' and str[j]!='\0' increment j by 1 end while set l=j-1 if str[k]!=' ' and str[k]!='\0' loop while k<=1 if str[k]==str[l] increment max by 1 if count<=max set index=i and count = max end if end if else set max = 0, count = -1 break end else increment k and i by 1 end loop whileend ifset i=jstep 3 -> end loop forstep 4 -> loop for i = index and i!=-1 && str[i]!=' ' && str[i]!='\0' and i++ print str[i]step 5 -> end loop forstop
示例#include <stdio.h>#include <string.h>int main(int argc, char const *argv[]) { char str[] = {"malayalam liemadameil iji"}; int i, k, l, j, max =0, index = -1, check = 0, count = 0; for(i=0; i<strlen(str); i++) { max = 0; k = i; j = i+1; while(str[j]!=' ' && str[j]!='\0'){ j++; } l = j-1; if(str[k]!=' ' && str[k]!='\0') { while(k<=l) { if (str[k]==str[l]) { max++; if(count<=max) { index = i; count = max; } } else { max = 0; count = -1; break; } k++; l--; } } i = j; } for (i = index; i!=-1 && str[i]!=' ' && str[i]!='\0'; i++) { printf("%c", str[i]); } return 0;}
输出如果我们运行上面的程序,它将生成以下输出。
liemadameil
以上就是在c程序中,将句子中最长的回文单词打印出来的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product