search
HomeBackend DevelopmentC++In a C program, print out the longest palindrome word in a sentence

In a C program, print out the longest palindrome word in a sentence

给定一个句子,挑战是从给定的句子中找到最长的回文

什么是回文?

回文是一个单词或序列,即使在之后其含义仍然保持不变反转字符串

示例 - Nitin,反转字符串后其含义保持不变。

挑战是从给定的句子中找到最长的回文。

喜欢的句子是:malayalam liemadameil iji

它包含三个回文词,但最长的是 - liemadameil

算法

START
STEP 1 -> Declare start variables I, j, k, l, max to 0, index to -1, check to 0, count to 0
Step 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]!=&#39; &#39; and str[j]!=&#39;\0&#39;
      Increment j by 1
   End While
   Set l=j-1
   IF str[k]!=&#39; &#39; and str[k]!=&#39;\0&#39;
      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 While
End If
Set i=j
Step 3 -> End Loop For
Step 4 -> Loop For i = index and i!=-1 && str[i]!=&#39; &#39; && str[i]!=&#39;\0&#39; and i++
   Print str[i]
Step 5 -> End Loop For
STOP

示例

#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]!=&#39; &#39; && str[j]!=&#39;\0&#39;){
         j++;
      }
      l = j-1;
      if(str[k]!=&#39; &#39; && str[k]!=&#39;\0&#39;) {
         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]!=&#39; &#39; && str[i]!=&#39;\0&#39;; i++) {
      printf("%c", str[i]);
   }
   return 0;
}

输出

如果我们运行上面的程序,它将生成以下输出。

liemadameil

The above is the detailed content of In a C program, print out the longest palindrome word in a sentence. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
php怎么实现Web页面的打印功能php怎么实现Web页面的打印功能Mar 24, 2023 pm 04:11 PM

PHP 打印功能实现的步骤和技巧 在 Web 开发的过程中,打印功能是相当重要的一种需求。相信大家都遇到过需要从网页中打印出某些内容的情况,比如收据、报告、合同等。本文将介绍如何使用 PHP 实现 Web 页面的打印功能。

ppt打印出来显示不全怎么解决ppt打印出来显示不全怎么解决Jul 06, 2023 pm 02:05 PM

ppt打印出来显示不全解决方法:1、检查页面设置,确保页面大小与打印纸张大小相匹配;2、调整缩放比例,尝试不同的缩放比例,直到能在打印预览中看到完整的幻灯片内容;3、调整文字框大小,选中文字框,然后拖动边框以调整大小,以确保文字能够完整显示在打印页面上;4、优化图片分辨率,使用图像编辑软件将图片的分辨率调整为适合打印的大小;5、打印预览,用打印预览来检查PPT内容是否完整显示。

如何在沉浸式阅读器中使用Microsoft Reader Coach如何在沉浸式阅读器中使用Microsoft Reader CoachMar 09, 2024 am 09:34 AM

在这篇文章中,我们将向你展示如何在WindowsPC上的沉浸式阅读器中使用Microsoft阅读教练。阅读指导功能帮助学生或个人练习阅读并培养他们的识字技能。你从阅读支持的应用程序中的一段或一份文档开始,基于此,你的阅读报告由阅读教练工具生成。阅读报告显示了阅读的准确性、阅读所用的时间、每分钟的正确单词数,以及你在阅读时发现最具挑战性的单词。你还将能够练习这些单词,这总体上有助于培养你的阅读技能。目前,仅有Office或Microsoft365(包括OneNoteforWeb和WordforWe

传真和打印有区别吗传真和打印有区别吗Nov 06, 2022 am 10:59 AM

传真和打印是有区别的,其区别有:1、传真是将文件通过传真机从一方传到较远地方的另一方,而打印是只在电脑上将文件或者材料打印出来;2、打印机种类繁多,主要功能就是打印,而传真机主要的核心功能是发送和接收;3、打印机只能从计算机上发送他们想要的文件,而两台打印机不能相互传输数据,而传真机是在电话的基础上增加数据传输功能,两台传真机可以相互发送文件,无需计算机也可以接收文件。

PHP表单处理:表单数据导出与打印PHP表单处理:表单数据导出与打印Aug 09, 2023 pm 03:48 PM

PHP表单处理:表单数据导出与打印在网站开发中,表单是不可或缺的一部分。当网站上的表单被用户填写并提交后,开发者需要对这些表单数据进行处理。本文将介绍如何使用PHP处理表单数据,并演示如何将数据导出为Excel文件和打印出来。一、表单提交与基本处理首先,需要创建一个HTML表单,供用户填写并提交数据。假设我们有一个简单的反馈表单,包含姓名、邮箱和评论。HTM

在C语言中编写一个程序来打印实心和空心菱形图案在C语言中编写一个程序来打印实心和空心菱形图案Aug 29, 2023 am 09:33 AM

程序说明打印如下所示的实心和空心菱形图案算法对于空心菱形-AccepttheNumberofRowsforHollowRhombusfromtheUserCreateaHollowRhombuscontainingthesamenumberofRowsspecifiedbytheUser.Printthefirstrowcontainingthenumberofstarssameasthenumberofrows.Printthesecondrowcontainingthefirstandlas

墨墨背单词怎么重新开始背呢?分享墨墨背单词重新背单词方法!墨墨背单词怎么重新开始背呢?分享墨墨背单词重新背单词方法!Mar 15, 2024 pm 03:28 PM

好奇墨墨背单词怎么重新开始背吗?墨墨背单词是一款十分好用的英语单词学习软件,用户可以根据英语水平和学习意向选择英语词库进行英语学习,还能通过例句和助记等方法更好的理解记忆单词等。有小伙伴背完单词了想重新开始背同样的词汇书,但不知道如何操作?小编今天为各位用户整理了墨墨背单词重新背单词方法!有帮助到你就快来下载吧!一、墨墨背单词怎么重新开始背呢?分享墨墨背单词重新背单词方法!1.打开墨墨背单词app,在复习页面中看到签到功能,选择当天日期。2.点击进入,之后会看到查看详情选项。3.跳转页面后,选择

在C语言中编写一个程序,打印出N个五角数的序列在C语言中编写一个程序,打印出N个五角数的序列Aug 25, 2023 pm 02:25 PM

程序说明五维体数是帕斯卡三角形的任意一行中第五个数字,从左到右或从右到左开始,起始于5项行14641。这种数字的前几个是1,5,15,35,70,126,210,330,495,715,1001,1365Pentatopenumbersbelongintheclassoffiguratenumbers,whichcanberepresentedasregular,discretegeometricpatterns.Theformulaforthenthpentatopicnumberis$$\l

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),