joj1008

[JPoints: 381, Sender!: 0/0]

1008: Go hang a salami, I


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K86581612Standard

Given a character string, determine if it is a palindrome. A palindrome is a word or phrase that reads the same forwards and backwards, like mom or noon. For our purposes, palindromes are case insensitive, so Bob and Anna are palindromes. Also, we are only concerned with alphabetic characters, so punctuation and other non-alphabetic characters may be ignored.

Input

A positive integer denoting the number of cases. Each case will consist of a string of characters, including blanks and non-alphabetic characters. Each case will be on a separate line.

Output

The sentence “<string> is a palindrome.” if the word is a palindrome, or the sentence“<string> is not a palindrome.” if it is not, where <string> is the input string.

Sample Input

5
Allen Alda
asdffdsa
Race car
Go hang a salami, I'm a lasagna hog!
Hanna

Sample Output

Allen Alda is not a palindrome.
asdffdsa is a palindrome.
Race car is a palindrome.
Go hang a salami, I'm a lasagna hog! is a palindrome.
Hanna is not a palindrome.

Notice: A string without alphabet should not be considered as a palindrome.

 


This problem is used for contest: 60


Submit / Problem List / Status / Discuss

 

 

刚开始做,WA了N次。觉得想法一点问题也没有,就是过不去。

后来发现是没考虑对于字符串里没有元素的情况(可是我觉得考虑这个情况没什么意义~~TT~~)

 1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4
5 bool ispalindrome(char a[]);
6
7 int main(void)
8 {
9
10 freopen("in.txt", "r", stdin);
11
12 char t[1000];
13 char a[1000];
14 int sample;
15
16 scanf("%d%*c", &sample);
17
18 for (int i=0; i<sample; i++)
19 {
20 gets(a);
21
22 int len = strlen(a);
23 a[len] = '\0';
24 int k = 0;
25 for (int j=0; j<len; j++)
26 {
27 if (isalpha(a[j]))
28 {
29 t[k++] = tolower(a[j]);
30 }
31 }
32 t[k] = '\0'; //注意不是t[k+1] = '\0'
33 if (ispalindrome(t))
34 {
35 printf("%s is a palindrome.\n", a);
36 }
37 else
38 {
39 printf("%s is not a palindrome.\n", a);
40 }
41 }
42
43
44
45 return 0;
46 }
47
48 bool ispalindrome(char a[])
49 {
50 int cnt = strlen(a);
51 if (0 == cnt)
52 {
53 return false;
54 }
55 for (int i=0, j=cnt-1; i<j; i++, j--)
56 {
57 if (a[i] != a[j])
58 {
59 return false;
60 }
61 }
62 return true;
63 }



转载于:https://www.cnblogs.com/RootJie/archive/2012/01/27/2330110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值