C++代码,如何把方阵中的数字改为用字母表示最外层的方阵?

C++中常用的大小写转换(4种常用方法)1、异或操作实现大小写转换因为大写字母与小写字母的ASCII相差刚好为32,则对应可以通过大写(小写)ASCII码与二进制(10000)进行按位异或转化为对应小写(大写)转换。如下://S为string类
S[i]
^= (1<<5);
//即可实现字符串S中第i+1位字符大小写转换
2、string类
#include <algorithm>
 transform(S.begin(),S.end(),str.begin(),::tolower);
 ```
其中S为string类,需要注意是**::tolower** 没有(); 将大写转为小写::toupper 用来将小写转为大写3、string类也可以自己手写两个转化为大写和小写transform()方法,其中大写与小写相差32,代码如下所示:#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
void mytolower(string& s){
int len=s.size();
for(int i=0;i<len;i++){
//string字符串可以通过下标[i]定位索引字符串其中第i+1个字符
if(s[i]>='A'&&s[i]<='Z'){
s[i]+=32;//+32转换为小写
//s[i]=s[i]-'A'+'a';
}
}
}
void mytoupper(string& s){
int len=s.size();
for(int i=0;i<len;i++){
if(s[i]>='a'&&s[i]<='z'){
s[i]-=32;//+32转换为小写
//s[i]=s[i]-'a'+'A';
}
}
4、如果用char数组,也可以自己手写两个转化为大写和小写方法,此种方法用到了tolower(char c)和toupper(char c)两个方法:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
void mytolower(char *s){
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]>='A'&&s[i]<='Z'){
s[i]=tolower(s[i]);
//s[i]+=32;//+32转换为小写
//s[i]=s[i]-'A'+'a';
}
}
}
void mytoupper(char *s){
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]>='a'&&s[i]<='z'){
s[i]=toupper(s[i]);
//s[i]-=32;//+32转换为小写
//s[i]=s[i]-'a'+'A';
}
}
}
5、如果用char数组,也可以使用s[i]+=32或者s[i]=s[i]-‘A’+'a’的形式,实现两个转化为大写和小写方法,}
#include<iostream>
#include<iomanip>
using namespace std;
void main(){
int i,j,a[100][100],n,k;
cout<<"input the width of the matrix:";
cin>>n;
k=1;
for(i=1;i<=n;i++){
for(j=1;j<=n+1-i;j++){
a[i-1+j][j]=k;
k=k+1;
}
}
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout<<setw(4)<<a[i][j]<<" ";
}
cout<<endl;
}
return ;
}
}

我要回帖

更多关于 字符正方形c语言代码 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信