C++字母大小写转换方法
来源:互联网 发布:达内软件培训机构 编辑:程序博客网 时间:2024/06/03 09:34
字母大小写这个问题相对比较简单,总结了一些常用的大小写转换的方法,欢迎指正补充!
思路1:根据字母的ASCII表进行转换:
由表格可以看出,对应大小写字母之间相差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:利用大小写字母转换函数,由此可以衍生出一下几种编程的思路:
程序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 利用函数strupr、strlwr
#include<iostream>#include<string>using namespace std;int main(int argc, char* argv[]){ //声明字符数组 char str[80],*p; int i; //转换字符串中的小写为大写 cout<<"将字符串中的小写字母转换为大写"<<endl; cout<<"请输入原字符串:"<<endl; cin>>str; p=strupr(str); cout<<"p:"<<p<<endl; cout<<"string:"<<str<<endl; cout<<"___________________"<<endl; //转换字符串中的大写为小写 cout<<"将字符串中的大写字母转换为小写"<<endl; cout<<"请输入原字符串:"<<endl; cin>>str; p=strlwr(str); cout<<"p:"<<p<<endl; cout<<"string:"<<str<<endl; cout<<"___________________"<<endl;system("pause");return 0;}
程序2.3 利用函数toupper、tolower
#include<iostream>#include<cctype>#include<vector>using namespace std;int main(){ vector<char> vch; int n; char elem; cout<<"请输入大小写字符的个数:"; 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<<"大小写转化之后的结果:"; vector<char>::iterator itera = vch.begin(); for(itera;itera != vch.end();++itera) cout<<*itera; cout<<endl; return 0;}
程序2.4 利用transform和tolower及toupper进行结合
#include<iostream>#include<algorithm>#include<string>#include<cctype>using namespace std;int main(){ cout<<"请输入一个全部大写的字符串:"; string str; cin>>str; ///转小写 transform(str.begin(),str.end(),str.begin(),tolower); ///transform(wstr.begin(), wstr.end(), wstr.begin(), towlower); cout<<"转化为小写后为:"<<str<<endl; ///转大写 cout<<"请再输入一个全部小写的字符串:"; string s; cin>>s; transform(s.begin(), s.end(), s.begin(), toupper); ///transform(wstr.begin(), wstr.end(), wstr.begin(), towupper); cout<<"转化为大写后为:"<<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函数,利用|=和&=进行变换
#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;}
当然还有其它方法,欢迎补充!
参考资料:
c++字符串大小写转换
c++大小写转换
C++大小写转换
toupper, _toupper, towupper, _toupper_l, _towupper_l
2 0
- 大小写字母转换方法
- 【c语言】大小写字母转换
- C语言大小写字母转换
- C++字母大小写转换方法
- C中大小写字母转换的问题
- C语言实现大小写字母的转换
- C语言基础 字母大小写转换
- C语言字符串中大小写字母转换
- C语言(字母大小写的转换)
- 字母大小写转换一另类方法
- C语言编程:字母大小写转换以及求字符串长度的两种方法
- 字母的大小写转换
- 转换字母的大小写
- js大小写字母转换
- 字母大小写转换
- java 大小写字母转换
- js字母大小写转换
- 大小写字母的转换
- 求数组中最长递增子序列
- Porting an Angular 2.0 App to Aurelia
- cell里面的回调,最后不要通过indexPath的属性来进行回调识别
- Educational Codeforces Round 8 F. Bear and Fair Set【最大流】
- 263. Ugly Number
- C++字母大小写转换方法
- KVO,NSNotification,delegate及block区别?
- Leetcode题解14 27. Remove Element
- Visio二次开发问题——如何在Visio右键中显示Shapesheet?
- input驱动12种事件类型Event types的含义
- Code Forces 26C Dijkstra?
- iOS开发数据库篇—SQLite简单介绍
- Java读取文件
- bochs调试