Abstract
This is an exercise and question that often occurs when I have been studying C.
Introduction
SpecifiedReplyThe first character of the string is equal to the last character, and the second character is equal to the second character.
Palindrome. C/C
1 /*
2 (C) oomusou 2008Http://oomusou.cnblogs.com
3
4 Filename: palindrome. c
5 Compiler: Visual C ++ 9.0/Visual Studio 2008
6 Description: Demo how to test palindrome
7 Release: 10/27/2008 1.0
8 */
9
10 # Include<Stdio. h>
11 # Include<String. H>
12
13 IntIs_palindrome (Char * S ){
14 Int I, Len;
15
16 Len= Strlen (s );
17
18 for (I = 0 ; I (LEN / 2 ); I + ){
19 If(S [I]! =S [Len-I-1 ])
20 Return 0 ;
21 }
22
23 If(I> =Len/2 )
24 Return 1 ;
25 Else
26 Return 0 ;
27 }
28
29 Int Main (){
30 CharS [256 ];
31
32 While(1 ){
33 Gets (s );
34
35 If (Is_palindrome (s ))
36 Printf ("% S is palindrome \ n" , S );
37 Else
38 Printf ("% S is not palindrome \ n" , S );
39 }
40 }
18 rows
For(I= 0; I<(Len/2); I++ ){
If(S [I]! =S [Len-I-1 ])
Return 0 ;
}
Check in sequence whether the first character of a string is equal to the last character, and whether the second character is equal to the second character. If any character is different, it is not a reply.
SenseviceMu FengIn C ++, there is a more refined attention method.
Palindrome. CPP/C ++
1 /*
2 (C) oomusou 2008Http://oomusou.cnblogs.com
3
4 Filename: palindrome. cpp
5 Compiler: Visual C ++ 9.0/Visual Studio 2008
6 Description: Demo how to test palindrome
7 Release: 10/30/2008 1.0
8 */
9
10 # Include<Iostream>
11 # Include<String>
12 # Include<Algorithm>
13
14 Using Namespace STD;
15
16 IntIs_palindrome (String S ){
17 ReturnEqual (S. Begin (), S. Begin ()+S. Length ()/2 , S. rbegin ());
18 }
19
20 Int Main (){
21 String S;
22
23 While(1 ){
24 CIN> S;
25
26 If (Is_palindrome (s ))
27 Cout<S< "Is palindrome" < Endl;
28 Else
29 Cout<S< "Is not palindrome" < Endl;
30 }
31 }
17 rows
ReturnEqual (S. Begin (), S. Begin ()+S. Length ()/2, S. rbegin ());
Use the EQUAL () function of STLAlgorithmIn a row, you can determine whether the token is a bounce message.