C++大小字母的轉(zhuǎn)換方式
C++大小字母轉(zhuǎn)換
因為所有小寫字母的ASCⅡ值要比對應(yīng)的大寫字母的ASCⅡ值大32,所以c1減去32后便得到原來字母的大寫形式;反之,c2加上32后便得到原來字母的小寫形式。
#include<iostream> using namespace std; //大小寫字母轉(zhuǎn)換通過ASCⅡ碼值進行改變,小寫-大寫=32; int main() { ?? ?char c1 = 'a'; ?? ?char c2 = 'A'; ?? ?cout << c1 << " ?" << c2 << endl; ?? ?c1 = c1 - 32;//小寫轉(zhuǎn)換成大寫 ?? ?c2 = c2 + 32;//大寫轉(zhuǎn)換成小寫 ?? ?cout << c1 << " ?" << c2 << endl; }
運行結(jié)果:
a A
A a
C++常用的大小寫轉(zhuǎn)換方法
思路1:根據(jù)字母的ASCII表進行轉(zhuǎn)換
由表格可以看出,對應(yīng)大小寫字母之間相差32,由此可以衍生出以下編程的思路:
程序1.1
#include <iostream> using namespace std; int main() { char a[20]; int i = 0; cout<<"請輸入一串字符:\n"; cin>>a; for(;a[i];i++) { if(a[i] >= 'a'&&a[i] <= 'z') a[i] -= 32; else if(a[i] >= 'A'&&a[i] <= 'Z') a[i] += 32; } for(i = 0;a[i];i++) cout<<a[i]; cout<<endl; system("pause"); return 0; }
程序1.2
#include <iostream> using namespace std; void main(void) { char i; cout<<"Input,'#'for an end: "<<endl; while(1) { cin >> i; if ((i>=65)&&(i<=90)) { i=i+32; cout << i; } else if((i>=97)&&(i<=122)) { i=i-32; cout << i; } else cout << (int)i; if(i=='#') break; } }
思路2:利用大小寫字母轉(zhuǎn)換函數(shù)
由此可以衍生出以下幾種編程的思路:
程序2.1 簡易版
#include <iostream> using namespace std; int main() { cout<<(char)toupper(97)<<'\n'; cout<<(char)toupper('a')<<'\n'; cout<<(char)tolower(66)<<'\n'; cout<<(char)tolower('B')<<'\n'; return 0; }
程序2.2 利用函數(shù)strupr、strlwr
#include<iostream> #include<string> using namespace std; int main(int argc, char* argv[]) { //聲明字符數(shù)組 char str[80],*p; int i; //轉(zhuǎn)換字符串中的小寫為大寫 cout<<"將字符串中的小寫字母轉(zhuǎn)換為大寫"<<endl; cout<<"請輸入原字符串:"<<endl; cin>>str; p=strupr(str); cout<<"p:"<<p<<endl; cout<<"string:"<<str<<endl; cout<<"___________________"<<endl; //轉(zhuǎn)換字符串中的大寫為小寫 cout<<"將字符串中的大寫字母轉(zhuǎn)換為小寫"<<endl; cout<<"請輸入原字符串:"<<endl; cin>>str; p=strlwr(str); cout<<"p:"<<p<<endl; cout<<"string:"<<str<<endl; cout<<"___________________"<<endl; system("pause"); return 0; }
程序2.3 利用函數(shù)toupper、tolower
#include<iostream> #include<cctype> #include<vector> using namespace std; int main() { vector<char> vch; int n; char elem; cout<<"請輸入大小寫字符的個數(shù):"; cin>>n; cout<<"請輸入"<<n<<"個大小寫字符:"; for(int i = 0;i<n;++i) { cin>>elem; vch.push_back(elem); } vector<char>::iterator it = vch.begin(); for(it;it != vch.end();++it) { if(*it >= 'a'&&(*it) <='z') *it = toupper(*it); else if(*it >= 'A'&& (*it) <= 'Z') *it = tolower(*it); } cout<<"大小寫轉(zhuǎn)化之后的結(jié)果:"; vector<char>::iterator itera = vch.begin(); for(itera;itera != vch.end();++itera) cout<<*itera; cout<<endl; return 0; }
程序2.4 利用transform和tolower及toupper進行結(jié)合
#include<iostream> #include<algorithm> #include<string> #include<cctype> using namespace std; int main() { cout<<"請輸入一個全部大寫的字符串:"; string str; cin>>str; ///轉(zhuǎn)小寫 transform(str.begin(),str.end(),str.begin(),tolower); ///transform(wstr.begin(), wstr.end(), wstr.begin(), towlower); cout<<"轉(zhuǎn)化為小寫后為:"<<str<<endl; ///轉(zhuǎn)大寫 cout<<"請再輸入一個全部小寫的字符串:"; string s; cin>>s; transform(s.begin(), s.end(), s.begin(), toupper); ///transform(wstr.begin(), wstr.end(), wstr.begin(), towupper); cout<<"轉(zhuǎn)化為大寫后為:"<<s; wstring wstr =L"Abc"; transform(wstr.begin(), wstr.end(), wstr.begin(), towupper); cout<<wstr; return 0; }
程序2.5 注意wmain(),另一種編程方法
#include <iostream> #include <cstring> #include <windows.h> #include <cctype> #include <algorithm> using namespace std; int wmain(int argc, WCHAR* argv[]) { char ch = 'a'; ch = toupper(ch); cout<<ch<<endl; WCHAR wch = 'a'; wch = towupper(wch); cout<<char(wch)<<endl; WCHAR wideStr[] = L"Abc"; _wcslwr_s(wideStr, wcslen(wideStr) + 1); _wcsupr_s(wideStr, wcslen(wideStr) + 1); wstring wstr =L"Abc"; transform(wstr.begin(), wstr.end(), wstr.begin(), towupper); return 0; }
程序2.6 寫成convert函數(shù),利用|=和&=進行變換
#include <iostream> #include <cassert> using namespace std; char* convert(char *src) { char *p = src; assert(p != NULL); while(*p) { if ('A' <= *p && *p < 'Z') *p |= 0x20; else if ('a' <= *p && *p < 'z') *p &= ~0x20; p++; } return src; } int main() { char src; cin>>src; convert(&src); cout<<src; return 0; }
其中,在用到transform時,可能遇到如下錯誤提示:
error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)’
這里說明了原因:
The problem is that the version of std::tolower inherited from the C standard library is a non-template function, but there are other versions of std::tolower that are function templates, and it is possible for them to be included depending on the standard library implementation. You actually want to use the non-template function, but there is ambiguity when just tolower is provided as the predicate.
翻譯過來就是說,既有C版本的toupper/tolower函數(shù),又有STL模板函數(shù)toupper/tolower,二者存在沖突。
解決辦法:
在toupper/tolower前面加::,強制指定是C版本的(這時也不要include <cctype>了):
#include <iostream> #include <string> #include <algorithm> // transform using namespace std; int main() { string str = "abcdADcdeFDde!@234"; transform(str.begin(), str.end(), str.begin(), ::toupper); cout << str << endl; transform(str.begin(), str.end(), str.begin(), ::tolower); cout << str << endl; return 0; }
關(guān)于transform
transform() 可以將函數(shù)應(yīng)用到序列的元素上,并將這個函數(shù)返回的值保存到另一個序列中,它返回的迭代器指向輸出序列所保存的最后一個元素的下一個位置。
這個算法有一個版本和 for_each() 相似,可以將一個一元函數(shù)應(yīng)用到元素序列上來改變它們的值,但這里有很大的區(qū)別。for_each() 中使用的函數(shù)的返回類型必須為 void,而且可以通過這個函數(shù)的引用參數(shù)來修改輸入序列中的值;而 transform() 的二元函數(shù)必須返回一個值,并且也能夠?qū)?yīng)用函數(shù)后得到的結(jié)果保存到另一個序列中。
不僅如此,輸出序列中的元素類型可以和輸入序列中的元素類型不同。對于 for_each(),函數(shù)總是會被應(yīng)用序列的元素上,但對于 transform(),這一點無法保證。
第二個版本的 transform() 允許將二元函數(shù)應(yīng)用到兩個序列相應(yīng)的元素上,但先來看一下如何將一元函數(shù)應(yīng)用到序列上。在這個算法的這個版本中,它的前兩個參數(shù)是定義輸入序列的輸入迭代器,第 3 個參數(shù)是目的位置的第一個元素的輸出迭代器,第 4 個參數(shù)是一個二元函數(shù)。這個函數(shù)必須接受來自輸入序列的一個元素為參數(shù),并且必須返回一個可以保存在輸出序列中的值。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于C/C++中的side effect(負效應(yīng))和sequence point(序列點)
不知你在寫code時是否遇到這樣的問題?int i = 3; int x = (++i) + (++i) + (++i); 問x值為多少?進行各種理論分析,并在編譯器上實踐,然而可能發(fā)現(xiàn)最終的結(jié)果是不正確的,也是不穩(wěn)定的,不同的編譯器可能會產(chǎn)生不同的結(jié)果。這讓人很頭疼2013-10-10C++ COM編程之QueryInterface函數(shù)(一)
這篇文章主要介紹了C++ COM編程之QueryInterface函數(shù)(一),QueryInterface是組件本身提供對自己查詢的一個接口,需要的朋友可以參考下2014-10-10C++ 設(shè)置和獲取當前工作路徑的實現(xiàn)代碼
這篇文章主要介紹了C++ 設(shè)置和獲取當前工作路徑的實現(xiàn)代碼,防止DLL加載不到配置和文件,需要的朋友可以參考下2017-09-09關(guān)于C++中的static關(guān)鍵字的總結(jié)
C++的static有兩種用法:面向過程程序設(shè)計中的static和面向?qū)ο蟪绦蛟O(shè)計中的static。前者應(yīng)用于普通變量和函數(shù),不涉及類;后者主要說明static在類中的作用2013-09-09c語言連接mysql數(shù)據(jù)庫的實現(xiàn)方法
C語言連接mysql數(shù)據(jù)庫,需要相應(yīng)的頭文件和lib文件,如果你安裝Mysql數(shù)據(jù)庫,會在安裝目錄下找到這些庫文件,如果沒有安裝,也可以在網(wǎng)上找到2012-05-05C++中的opeartor?new和placement?new使用步驟
這篇文章主要介紹了C++中的opeartor?new和placement?new詳解,在很多情況下,placement?new的使用方法和其他普通的new有所不同。這里提供了它的使用步驟,需要的朋友可以參考下2022-10-10