(筆記) 如何判斷回文(palindrome) ? (C/C++) (C) (STL)
2008-10-27 01:26
344 查看
Abstract
回文是學習C語言時,常出現的作業與考題。
Introduction
所謂回文,就事字串的第一個字元等於最後一個字元,第二個字元等於倒數第二個字元。
palindrome.c / C
1 /*
2 (C) OOMusou 2008 http://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 int is_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 char s[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行
for(i = 0; i < (len /2); i++) {
if (s[i] != s[len-i-1])
return 0;
}
依序檢查字串第一個字元是否等於最後一個字元,第二個字元是否等於倒數第二個字元,若發現任何一個字元不同,就不是回文。
感謝沐枫的提醒,在C++有更精簡的寫法。
palindrome.cpp / C++
1 /*
2 (C) OOMusou 2008 http://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 int is_palindrome(string s) {
17 return equal(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行
return equal(s.begin(), s.begin() + s.length() /2, s.rbegin());
使用STL的equal()演算法,一行就可以判斷是不是回文了。
回文是學習C語言時,常出現的作業與考題。
Introduction
所謂回文,就事字串的第一個字元等於最後一個字元,第二個字元等於倒數第二個字元。
palindrome.c / C
1 /*
2 (C) OOMusou 2008 http://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 int is_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 char s[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行
for(i = 0; i < (len /2); i++) {
if (s[i] != s[len-i-1])
return 0;
}
依序檢查字串第一個字元是否等於最後一個字元,第二個字元是否等於倒數第二個字元,若發現任何一個字元不同,就不是回文。
感謝沐枫的提醒,在C++有更精簡的寫法。
palindrome.cpp / C++
1 /*
2 (C) OOMusou 2008 http://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 int is_palindrome(string s) {
17 return equal(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行
return equal(s.begin(), s.begin() + s.length() /2, s.rbegin());
使用STL的equal()演算法,一行就可以判斷是不是回文了。
相关文章推荐
- 如何判斷回文(palindrome) ? (C/C++) (C) (STL)
- (筆記) 如何將10進位轉2進位? (C/C++) (C) (STL)
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
- URAL 1297. Palindrome(输出最长回文子串--后缀数组)
- FAQ14: STL中vector容器如何间隔删除元素?
- 如何判斷一個查詢結果是否為空?
- (原創) 如何將std::string轉成大寫(小寫)? (C/C++) (STL) (C)
- GDB pretty printer: linux下用gdb调试c++时如何更好的查看STL容器值
- 如何使for_each()傳回值? (C/C++) (STL)
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值
- [LeetCode] Palindrome Permutation II 回文全排列之二
- [经验总结]如何在VC中去掉STL代码编译时编译错误
- c++如何让自定义的类可以执行STL中的sort算法
- 【转】如何在Android平台下编译带STL的C++程序
- (筆記) 如何設計4位元的加法器? (SOC) (Verilog) (MegaCore)
- Ural 1297 Palindrome(后缀数组+最长回文子串)
- (原創) 如何将字符串前后的空白去除? (C/C++) (STL)
- 判断回文的函数palindrome?
- (原創) 如何為程式碼加上行號? (C/C++) (STL)
- 如何运行C++ STL程序