palindrome

class Solution {
public:
    bool isPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
         	transform(s.begin(), s.end(), s.begin(), ::toupper);
		string ss;
		for ( int i = 0; i != s.length(); ++i)
		{
			if ( isalnum(s[i]) )
				ss.push_back(s[i]);
		}


		//普通意义下的回文
		string sss;
		sss.assign(ss.rbegin(),ss.rend());
		if(ss==sss)
			return true;
		else
			return false;
        
    }
};

  

 

C++中处理string对象的字符

反向迭代器(rbegin,rend)

http://www.vimer.cn/2009/11/string转化大小写c.html

:

如何将一个字符串转换成大写或者小写?这是字符串匹配中经常需要做的事情,然而C++的Standard Library并没有提供将std::string转成大写和小写的功能,只有在提供将char转成大写(toupper)和小写(tolower)的功能而已。
但我们可以利用STL的transform配合toupper/tolower,完成std::string转换大(小)写的功能,也看到 模版编程 的威力了,一个transform函数,可以适用于任何类型,且只要自己提供 函数 ,就可完成任何Transform的动作。
C++

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
int main() {
    string s = "Clare";
    // toUpper
    transform(s.begin(), s.end(), s.begin(), ::toupper);
    // toLower
    //transform(s.begin(),s.end(),s.begin(), ::tolower);
    cout << s << endl;
}

C

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <ctype.h>
int main() {
    char s[] = "Clare";
    int i = -1;
    while(s[i++]) 
        s[i] = toupper(s[i]);
    // s[i] = tolower(s[i]);
    puts(s);  
}

 

 

原文链接: https://www.cnblogs.com/chunlifang-luck/archive/2013/05/11/3073030.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    palindrome

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88037

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午11:24
下一篇 2023年2月9日 下午11:24

相关推荐