isPalindrome-first day   
               添加时间:2013-6-25 点击量: 
 
              practice for work~~~~~~~
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 that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 bool isPalindrome(string s) {
 7     if(s.length()<= 1)
 8         return true;
 9     
10     int n= s.length();   // here can not be size_t
11     char b,c;
12     
13     
14     for(int i=0,j=n-1;j>i;i++, j--)
15     {
16         b = s.at(i);
17         while(b<48 || (b>57 && b < 97) || b> 122 )    // here i>n may happen
18         {
19             if (i>=j)  // there may be i> n, so...
20                 return true;
21             if (b>=A && b <=Z)
22                 b=b+32;
23             else
24                 b = s.at(++i);
25         }
26         
27          c = s.at(j);
28         while(c<48 || (c>57 && c < 97) || c> 122)
29         {
30             if (j<=i)
31                 return true;
32             if (c>=A && c <=Z)
33                 c=c+32;
34             else
35                 c = s.at(--j);
36         }
37         if(b != c)
38             return false;
39     }
40     return true;
41 }
42 
43 int main()
44 {
45     string s=1a2;
46     
47     cout<<isPalindrome(s)<<endl;
48     
49     return 0;   
50 }
 1 #include<string.h>
 2 #include<stdlib.h>
 3 #include<stdio.h>
 4 
 5 
 6  bool isPalindrome(char str) {
 7      if (strlen(str)<=1)
 8      {
 9          return true;
10      }
11      
12      char s= str;
13      char p = str+strlen(str)-1;
14      
15      while(p>s)
16      {
17          while(s<48 || (s>57 && s < 97) || s> 122)
18          {
19              if (s>=p)
20                  return true;
21              if (s>=A && s <=Z)
22                  s=s+32;
23              else
24                  s++;
25          }
26          while(p<48 || (p>57 && p < 97) || p> 122)
27          {
28              if (s>=p)
29                  return true;
30              if (p>=A && p <=Z)
31                  p=p+32;
32              else
33                  p--;
34          }
35          if(s != p)
36          {
37              return false;
38          }
39          s++;
40          p--;
41      }
42      
43      return true;
44 
45  }
46 
47 int main()
48 {
49    char s[]=1a1.;
50     printf(%d, isPalindrome(s));
51     return 0;
52 }
文艺不是炫耀,不是花哨空洞的文字堆砌,不是一张又一张的逆光照片,不是将旅行的意义转化为名牌包和明信片的物质展示;很多时候它甚至完全不美——它嘶吼、扭曲,它会痛苦地抽搐,它常常无言地沉默。——艾小柯《文艺是一种信仰》
                     
                  
     
  
 
    
    
practice for work~~~~~~~
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 that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 bool isPalindrome(string s) {
7 if(s.length()<= 1)
8 return true;
9
10 int n= s.length(); // here can not be size_t
11 char b,c;
12
13
14 for(int i=0,j=n-1;j>i;i++, j--)
15 {
16 b = s.at(i);
17 while(b<48 || (b>57 && b < 97) || b> 122 ) // here i>n may happen
18 {
19 if (i>=j) // there may be i> n, so...
20 return true;
21 if (b>=A && b <=Z)
22 b=b+32;
23 else
24 b = s.at(++i);
25 }
26
27 c = s.at(j);
28 while(c<48 || (c>57 && c < 97) || c> 122)
29 {
30 if (j<=i)
31 return true;
32 if (c>=A && c <=Z)
33 c=c+32;
34 else
35 c = s.at(--j);
36 }
37 if(b != c)
38 return false;
39 }
40 return true;
41 }
42
43 int main()
44 {
45 string s=1a2;
46
47 cout<<isPalindrome(s)<<endl;
48
49 return 0;
50 }
1 #include<string.h>
2 #include<stdlib.h>
3 #include<stdio.h>
4
5
6 bool isPalindrome(char str) {
7 if (strlen(str)<=1)
8 {
9 return true;
10 }
11
12 char s= str;
13 char p = str+strlen(str)-1;
14
15 while(p>s)
16 {
17 while(s<48 || (s>57 && s < 97) || s> 122)
18 {
19 if (s>=p)
20 return true;
21 if (s>=A && s <=Z)
22 s=s+32;
23 else
24 s++;
25 }
26 while(p<48 || (p>57 && p < 97) || p> 122)
27 {
28 if (s>=p)
29 return true;
30 if (p>=A && p <=Z)
31 p=p+32;
32 else
33 p--;
34 }
35 if(s != p)
36 {
37 return false;
38 }
39 s++;
40 p--;
41 }
42
43 return true;
44
45 }
46
47 int main()
48 {
49 char s[]=1a1.;
50 printf(%d, isPalindrome(s));
51 return 0;
52 }
文艺不是炫耀,不是花哨空洞的文字堆砌,不是一张又一张的逆光照片,不是将旅行的意义转化为名牌包和明信片的物质展示;很多时候它甚至完全不美——它嘶吼、扭曲,它会痛苦地抽搐,它常常无言地沉默。——艾小柯《文艺是一种信仰》




