C++大小字母的轉(zhuǎn)換方式
C++大小字母轉(zhuǎn)換
因?yàn)樗行?xiě)字母的ASCⅡ值要比對(duì)應(yīng)的大寫(xiě)字母的ASCⅡ值大32,所以c1減去32后便得到原來(lái)字母的大寫(xiě)形式;反之,c2加上32后便得到原來(lái)字母的小寫(xiě)形式。
#include<iostream>
using namespace std;
//大小寫(xiě)字母轉(zhuǎn)換通過(guò)ASCⅡ碼值進(jìn)行改變,小寫(xiě)-大寫(xiě)=32;
int main()
{
?? ?char c1 = 'a';
?? ?char c2 = 'A';
?? ?cout << c1 << " ?" << c2 << endl;
?? ?c1 = c1 - 32;//小寫(xiě)轉(zhuǎn)換成大寫(xiě)
?? ?c2 = c2 + 32;//大寫(xiě)轉(zhuǎn)換成小寫(xiě)
?? ?cout << c1 << " ?" << c2 << endl;
}運(yùn)行結(jié)果:
a A
A a
C++常用的大小寫(xiě)轉(zhuǎn)換方法
思路1:根據(jù)字母的ASCII表進(jìn)行轉(zhuǎn)換

由表格可以看出,對(duì)應(yīng)大小寫(xiě)字母之間相差32,由此可以衍生出以下編程的思路:
程序1.1
#include <iostream>
using namespace std;
int main()
{
char a[20];
int i = 0;
cout<<"請(qǐng)輸入一串字符:\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:利用大小寫(xiě)字母轉(zhuǎn)換函數(shù)
由此可以衍生出以下幾種編程的思路:
程序2.1 簡(jiǎn)易版
#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)換字符串中的小寫(xiě)為大寫(xiě)
cout<<"將字符串中的小寫(xiě)字母轉(zhuǎn)換為大寫(xiě)"<<endl;
cout<<"請(qǐng)輸入原字符串:"<<endl;
cin>>str;
p=strupr(str);
cout<<"p:"<<p<<endl;
cout<<"string:"<<str<<endl;
cout<<"___________________"<<endl;
//轉(zhuǎn)換字符串中的大寫(xiě)為小寫(xiě)
cout<<"將字符串中的大寫(xiě)字母轉(zhuǎn)換為小寫(xiě)"<<endl;
cout<<"請(qǐng)輸入原字符串:"<<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<<"請(qǐng)輸入大小寫(xiě)字符的個(gè)數(shù):";
cin>>n;
cout<<"請(qǐng)輸入"<<n<<"個(gè)大小寫(xiě)字符:";
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<<"大小寫(xiě)轉(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進(jìn)行結(jié)合
#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>
using namespace std;
int main()
{
cout<<"請(qǐng)輸入一個(gè)全部大寫(xiě)的字符串:";
string str;
cin>>str;
///轉(zhuǎn)小寫(xiě)
transform(str.begin(),str.end(),str.begin(),tolower);
///transform(wstr.begin(), wstr.end(), wstr.begin(), towlower);
cout<<"轉(zhuǎn)化為小寫(xiě)后為:"<<str<<endl;
///轉(zhuǎn)大寫(xiě)
cout<<"請(qǐng)?jiān)佥斎胍粋€(gè)全部小寫(xiě)的字符串:";
string s;
cin>>s;
transform(s.begin(), s.end(), s.begin(), toupper);
///transform(wstr.begin(), wstr.end(), wstr.begin(), towupper);
cout<<"轉(zhuǎn)化為大寫(xiě)后為:"<<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 寫(xiě)成convert函數(shù),利用|=和&=進(jìn)行變換
#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時(shí),可能遇到如下錯(cuò)誤提示:
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>)’
這里說(shuō)明了原因:
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.
翻譯過(guò)來(lái)就是說(shuō),既有C版本的toupper/tolower函數(shù),又有STL模板函數(shù)toupper/tolower,二者存在沖突。
解決辦法:
在toupper/tolower前面加::,強(qiáng)制指定是C版本的(這時(shí)也不要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)用到序列的元素上,并將這個(gè)函數(shù)返回的值保存到另一個(gè)序列中,它返回的迭代器指向輸出序列所保存的最后一個(gè)元素的下一個(gè)位置。
這個(gè)算法有一個(gè)版本和 for_each() 相似,可以將一個(gè)一元函數(shù)應(yīng)用到元素序列上來(lái)改變它們的值,但這里有很大的區(qū)別。for_each() 中使用的函數(shù)的返回類型必須為 void,而且可以通過(guò)這個(gè)函數(shù)的引用參數(shù)來(lái)修改輸入序列中的值;而 transform() 的二元函數(shù)必須返回一個(gè)值,并且也能夠?qū)?yīng)用函數(shù)后得到的結(jié)果保存到另一個(gè)序列中。
不僅如此,輸出序列中的元素類型可以和輸入序列中的元素類型不同。對(duì)于 for_each(),函數(shù)總是會(huì)被應(yīng)用序列的元素上,但對(duì)于 transform(),這一點(diǎn)無(wú)法保證。
第二個(gè)版本的 transform() 允許將二元函數(shù)應(yīng)用到兩個(gè)序列相應(yīng)的元素上,但先來(lái)看一下如何將一元函數(shù)應(yīng)用到序列上。在這個(gè)算法的這個(gè)版本中,它的前兩個(gè)參數(shù)是定義輸入序列的輸入迭代器,第 3 個(gè)參數(shù)是目的位置的第一個(gè)元素的輸出迭代器,第 4 個(gè)參數(shù)是一個(gè)二元函數(shù)。這個(gè)函數(shù)必須接受來(lái)自輸入序列的一個(gè)元素為參數(shù),并且必須返回一個(gè)可以保存在輸出序列中的值。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言嵌入informix基礎(chǔ)入門(mén)示例講解
這篇文章主要介紹了C語(yǔ)言嵌入informix基礎(chǔ)方法,大家參考使用2013-11-11
Dev C++編譯時(shí)運(yùn)行報(bào)錯(cuò)source file not compile問(wèn)題
這篇文章主要介紹了Dev C++編譯時(shí)運(yùn)行報(bào)錯(cuò)source file not compile問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
OpenCV實(shí)現(xiàn)物體的凸包檢測(cè)的示例代碼
給定二維平面上的點(diǎn)集,凸包就是將最外層的點(diǎn)連接起來(lái)構(gòu)成的凸邊形,它是包含點(diǎn)集中所有的點(diǎn)。本文將利用OpenCV實(shí)現(xiàn)物體的凸包檢測(cè),感興趣的可以了解一下2022-08-08
C++報(bào)錯(cuò)`Null Pointer Dereference`的解決方法
在軟件開(kāi)發(fā)中,Null Pointer Dereference 是一種常見(jiàn)的錯(cuò)誤,它發(fā)生在程序試圖訪問(wèn)或操作一個(gè)空指針指向的內(nèi)存位置時(shí),這種情況通常會(huì)導(dǎo)致程序崩潰,給 debug 工作帶來(lái)很大困擾,今天,我們將探討如何解決 Null Pointer Dereference 報(bào)錯(cuò),需要的朋友可以參考下2024-07-07
Species Tree 利用HashTable實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了Species Tree 利用HashTable實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01

