C++简介:

C++是C语言的继承,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于过程的程序设计,因而C++就适应的问题规模而论,大小由之。

C++不仅拥有计算机高效运行的实用性特征,同时还致力于提高大规模程序的编程质量与程序设计语言的问题描述能力。

C++大小写字母转换的思路有以下几种:

思路1、根据字母的ASCII表进行转换:

20160416123048063.jpg

由表格可以看出,对应大小写字母之间相差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(){    //声明字符数组    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 写成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语言教程》

更多相关文章

  1. c语言字符数组与字符串应用方法是什么?
  2. C语言怎么定义字符串数组
  3. C语言中的字符串比较函数是什么
  4. 一个c源程序中至少应包括一个什么函数
  5. C程序的注释只能是一行吗?
  6. 小白程序员C++入门学习书籍(书单)
  7. 新手程序员应该知道的C语言和C++的区别
  8. C语言程序的模块化通过什么实现
  9. c程序中宏展开是在什么时候进行的?

随机推荐

  1. Python:内联if语句别无效
  2. 详解高速神器python脚步打包android apk,
  3. qpython 读入数据问题: EOF error with in
  4. 使用python 3.6将多个文件并行加载到内存
  5. Python学习札记(二十六) 函数式编程7 修
  6. Python自然语言处理实践: 在NLTK中使用斯
  7. Python基本数据结构
  8. caffe python批量抽取图像特征
  9. 【原创】Python处理海量数据的实战研究
  10. 从正则表达式中浏览和提取字符类