How can I determine palindrome )? (C/C ++) (C) (STL)

Source: Internet
Author: User

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

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 */

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

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 */

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.