#include <stdio.h> #include <string.h> int main(){ int i,j; char a[1005]; while(scanf("%s",a) != EOF){ int flag=0; for(i=0,j=strlen(a)-1; i<=j; i++,j--){ if(a[i] != a[j]){ printf("No!\n"); flag=1; break; } } if(flag==0){ printf("Yes!\n"); } } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String str = in.nextLine(); boolean isPal = true; for(int i = 0, j = str.length()-1; i < j; i++, j--){ if(str.charAt(i) != str.charAt(j)){ System.out.println("No!"); isPal = false; break; } } if(isPal){ System.out.println("Yes!"); } } } }
import java.util.Scanner; /** *回文串就是一串字符正反序一样,所以将原来的字符串倒叙以后和原先字符串匹配则就是回文串 *字符串倒叙可直接用str.reverse() */ public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNext()){ String str=scan.nextLine(); StringBuilder sb=new StringBuilder(str); String newStr = sb.reverse().toString(); if(str.equals(newStr)){ System.out.println("Yes!"); }else{ System.out.println("No!"); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.next(); System.out.println(new StringBuilder(s).reverse().toString().equals(s)?"Yes!":"No!"); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String s = scanner.nextLine(); int i = 0; int j = s.length() - 1; while (i <= j) { if (s.charAt(i) == s.charAt(j)) { i++; j--; } else break; } System.out.println(i > j ? "Yes!" : "No!"); } } }
#include <stdio.h> #include <stdlib.h> int main() { char s[1000]; scanf("%s", s); int n = 0; while(s[n] != '\0') { n++; } int len = n-1; int i = 0, j = len; int flag = 1; while(i < j) { if(s[i++] != s[j--]) { flag = 0; break; } } flag == 1? printf("Yes!"):printf("No!"); return 0; }
#include <iostream> #include <string> using namespace std; int main(){ string str; bool flag; while(cin>>str){ string::size_type pos1=0,pos2=str.size()-1; flag=true; while(pos1<=pos2){ if(str[pos1++]!=str[pos2--]){ flag=false; } } (flag)?cout<<"Yes!"<<endl:cout<<"No!"<<endl; } return 0; }
#include<stdio.h> (737)#include<string.h> char str[1010]; void isPalindrome(char str[],int n){ int flag = 1; for(int i=0;i<n/2;i++){ if(str[i] != str[n-1-i]){ flag = 0; break; } } if(flag) printf("Yes!\n"); else printf("No!\n"); } int main(){ while(scanf("%s",str)!=EOF){ isPalindrome(str,strlen(str)); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ char ch[1001]; while(cin>>ch){ bool judge=1; for(int i=0;i<strlen(ch)/2&&judge;i++) if(ch[i]!=ch[strlen(ch)-1-i]) judge=0; if(judge) cout<<"Yes!"<<endl; else cout<<"No!"<<endl; } }
#include<stdio.h> #include<string.h> int main() { char str[1008]; char * start, * end; while(gets(str) != NULL){ start = str; end = str + strlen(str) - 1; while(end - start >= 0 && *start == *end){ start++; end--; } if(end - start < 0) printf("Yes!\n"); else printf("No!\n"); } return 0; }
#include <cstring> #include <cstdio> using namespace std; int main() { //freopen("date.txt", "r", stdin); char str[1001]; int i, j; while(scanf("%s", str) != EOF){ for(i = 0, j = strlen(str) - 1; i <= j; i++, j--) if(str[i] != str[j]){ printf("No!\n"); break; } if(i > j) printf("Yes!\n"); } return 0; }
#include <iostream> #include <string> using namespace std; int main(){ string str; while(cin>>str){ bool flag=true; int i,j; for(j=str.size()-1,i=0;j>=0;j--,i++){ if(str[j]!=str[i]){ flag=false; break; } } if(flag) cout<<"Yes!"<<endl; else cout<<"No!"<<endl; } return 0; }
try: while True: line = raw_input() # 读取输入 length = len(line) i, j = 0, length-1 while i < j: if line[i] != line[j]: print 'No!' break i += 1 j -= 1 if i >= j: print 'Yes!' except: pass
也可以把字符串直接反转。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string str1;
if (!string.IsNullOrWhiteSpace(str1 = Console.ReadLine()))
{
for (int i = 0; i < str1.Length; i++)
{
if (str1[i] != str1[str1.Length - i - 1])
{
Console.WriteLine("No!");
return;
}
}
Console.WriteLine("Yes!");
}
}
}
}