字符串-判断回文

编程判断一个字符串是否是回文,当字符串是回文时,输出字符串:yes!,
否则输出字符串:no!。所谓回文即正向与反的拼向写都一样,如adgda。  
长度在100以内,且全为小写字母
样例输入:
adgda
样例输出:
yes!

代码如下:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4
 5 int main()
 6 {
 7     char s[100];
 8     cin >> s;
 9     int n = strlen(s), flag=1;
10     for(int i=0;i<n/2;i++)
11     {
12         if(s[i]!=s[n-1-i])
13         {
14             cout << "no!" << endl;
15             flag = 0;
16             break;
17         }
18     }
19
20     if(flag) cout << "yes!" << endl;
21 }

时间: 2024-08-30 03:01:34

字符串-判断回文的相关文章

单链表字符串判断回文

思路 使用快慢两个指针找到链表中点,慢指针每次前进一步,快指针每次前进两步 在慢指针前进的过程中,同时修改其 next 指针,使得链表前半部分反序. 最后比较中点两侧的链表是否相等 c版本代码见 https://github.com/hkui/algo_practice/tree/master/c/linklist/palindrome_str java版本 https://github.com/andavid/leetcode-java/blob/master/note/234/README.

Java 判断回文字符串有多少和其中的最大字符串

一.简介代码功能 该代码的功能可以实现对任意的一段字符串进行判断是否有回文,回文有哪些,和其中的最大回文. 二.代码部分 1.全局变量 1 static String hws = ""; 2 static int num = 0; 3 static String[] hw; 2.创建数组用于保存回文 1 /** 2 * 创建数组保存所有的回文 3 * 4 * @return 返回一个String类型的数组 5 */ 6 public static String[] createHw()

golang 递归判断回文字符串

判断回文字符串是个比较经典的问题. 思路就是拿第一个字符和最一个字符比较,如果不等退出,相同的话继续刚刚的过程,直到第一个字符和最后一个字符相遇或者他们的距离为1时.说明他们是回文字符串. 下面的代码会忽略空白字符 如"1   1  2 1"会让为是回文字符串. golang package main import (     "fmt"     "os"     "strings"     "unicode/utf

【c语言】判断一个字符串是不是回文字符串

//判断一个字符串是不是回文字符串 #include <stdio.h> #include <assert.h> int panduan( char *p ) { char *q ; assert( *p != NULL ); q = p; while( *p != '\0') { p++; } p--; while(*q != '\0') { if( *p == *q) { p--; q++; } else return -1; } return 1; } int main()

Power oj/2610[判断回文串]

题目链接[https://www.oj.swust.edu.cn/problem/show/2610] 题意:给你一个字符串,让你判断这个字符串是不是回文串,字符串的长度是1<len<1e7,内存是4096KB. 题解:首先这1e7个字符是存不下的,1e71024=9765KB>4096kB.那么怎么办?字符串哈希,先对字符串的前半部分进行哈希,然后在对字符串后半部分进行哈希,如果两部分的哈希值相同,那么这个字符串就是回文串. BKDRH哈希,哈希公式为has=has*seed+s[i]

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

(LeetCode)Palindrome Number -- 判断回文数

Determine whether an integer is a palindrome. Do this without extra space. 解题分析: 题目很简单,但是对于要求需要看清楚. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ex

[程序员代码面试指南]字符串问题-回文最少分割数(DP)

问题描述 给定一个字符串,输出把它全部切成回文子串的最小分割数. 例:str="ACDCDCDAD",输出2. 解题思路 DP 存储结构 dp数组dp[len+1],dp[i]表示子串str[I:len]至少需要切割几次,才能都切割成回文串.对应的,循环从右至左进行. 注意 dp[i]的含义完全可以做对称更改,循环也变为从前向后即可. 此外,为了保证int cutTime=dp[j+1]+1;//句的顺利执行,且dp[len-1]=0,palStr=true.dp数组多开一位,并初始化