c++轉(zhuǎn)換字母大小寫的方法有幾種?針對這個問題,今天小編總結(jié)這篇有關(guān)字母大小寫轉(zhuǎn)換的文章,希望能幫助更多想解決這個問題的朋友找到更加簡單易行的辦法。

C++簡介:
C++是C語言的繼承,它既可以進(jìn)行C語言的過程化程序設(shè)計,又可以進(jìn)行以抽象數(shù)據(jù)類型為特點的基于對象的程序設(shè)計,還可以進(jìn)行以繼承和多態(tài)為特點的面向?qū)ο蟮某绦蛟O(shè)計。C++擅長面向?qū)ο蟪绦蛟O(shè)計的同時,還可以進(jìn)行基于過程的程序設(shè)計,因而C++就適應(yīng)的問題規(guī)模而論,大小由之。
C++不僅擁有計算機高效運行的實用性特征,同時還致力于提高大規(guī)模程序的編程質(zhì)量與程序設(shè)計語言的問題描述能力。
C++大小寫字母轉(zhuǎn)換的思路有以下幾種:
思路1、根據(jù)字母的ASCII表進(jìn)行轉(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()
{
//聲明字符數(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進(jìn)行結(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 寫成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;
}看完上述內(nèi)容,你們掌握c++轉(zhuǎn)換字母大小寫的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞標(biāo)題:c++轉(zhuǎn)換字母大小寫的方法有幾種-創(chuàng)新互聯(lián)
本文地址:http://www.chinadenli.net/article32/igesc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)、App開發(fā)、電子商務(wù)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)