-2
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() 中的从低到高的逻辑

4

2 回答 2

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;
            }
        }
    }       
}
于 2017-10-16T20:41:24.777 回答
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;
}

}

于 2017-12-29T21:40:59.113 回答