int main(){
char text[500];
int j,h,op;
char b[]=" abcdefghijklmnopqrstuvwxyz";
char a[]=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout<<"insert text:";fflush(stdin);gets(texto);
system("cls");
cout<<"1-Minus to Mayus"<<endl;
cout<<"2-Mayus to Minus"<<endl;
cin>>op;
system("cls");
if (op==1)
{
j=0;
h=0;
while(j<28){
if(text[h]==b[j]){
text[h]=a[j];
h++;
j=0;
}
j++;
}
cout<<text<<endl;
system("pause");
}
else if (op==2)
{
j=0;
h=0;
while(j<28){
if(text[h]==a[j]){
text[h]=b[j];
h++;
j=0;
}
j++;
}
cout<<text<<endl;
system("pause");
}
}
这个代码只适用于单个世界(直到第一个空格),我希望它运行整个句子甚至是段落。我希望你能理解下到上功能的逻辑main()的
答案 0 :(得分:2)
如果您不希望此代码依赖于ASCII表的值(尽管这可能会让您的生活变得更轻松),请遵循您有趣的代码精神,将字母转换为小写可以这样实现:
const char LOWERCASES[] = " abcdefghijklmnopqrstuvwxyz";
const char CAPITALS[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void toLowerCase(string &s) {
for (unsigned int i = 0; i < s.size(); ++i) {
for (unsigned int l = 0; l < 27; ++i) {
if (s[i] == CAPITALS[l]) {
s[i] = LOWERCASES[i];
break;
}
}
}
}
答案 1 :(得分:0)
试试这个
int main(){
int ss,T;
char text[100];
cout<<"Insert text ";gets(text);
T=tam(texto);
cout<<"1)Menu "<<endl;
cout<<"2)Upercase "<<endl;
cout<<"3)LowerCase "<<endl;
cout<<"4)Random"<<endl;cin>>ss;
switch(ss){
case 2:
for(int i=0 ; i<T ; i++){
if(text[i]>=97 && text[i]<=122)
text[i]=int(text[i])-32;
}
cout<<text<<endl;
break;
case 3:
for(int i=0 ; i<T ; i++){
if(text[i]>=65 && text[i]<=90)
text[i]=int(text[i])+32;
}
cout<<text<<endl;
break;
case 4:
for(int i=0 ; i<T ; i++){
if(text[i]>=97 && text[i]<=122)
text[i]=int(text[i])-32;
else
text[i]=int(text[i])+32;
}
cout<<text<<endl;
break;
}
}