简体   繁体   English

C++:如何在没有 ctype 函数的情况下将小写转换为大写,反之亦然?

[英]C++: How to convert lowercase to upercase and viceversa with out ctype functions?

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");
                }
            }

This code works just with a single world (until the first space), I want it to run with a whole sentence or even a paragraph.I hope that you could understand the logic of lower to upper function in the main()这段代码只适用于一个世界(直到第一个空格),我希望它运行整个句子甚至一个段落。我希望你能理解 main() 中从下到上函数的逻辑

If it is the case that you do not want this code to depend on the values of an ASCII table (although this could make your life much easier), following the spirit of your interestingly fashioned code, a function to convert letters to lowercase could be implemented as such:如果您不希望此代码依赖于 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;
            }
        }
    }       
}

Try this尝试这个

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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 for 循环打印 abc 大写和小写 C++ - print abc upercase and lowercase c++ using for loop 将ctype宏转换为c ++常量的方法 - method to convert ctype macros to c++ constants ctype函数(isalpha,isdigit)在c ++中显示错误的值 - ctype functions(isalpha, isdigit) show wrong values in c++ 如何在C ++中为ISO-8859-15创建自定义ctype - How to create custom ctype for iso-8859-15 in c++ 如何使单独的功能计数。 大写,小写,空格和特殊字符? C ++ - How make separate functions to count no. of uppercase, lowercase, spaces and special characters? C++ 如何使用for_each,transform,iterators和lambda的某种组合将字符串std :: set转换为小写c ++? - How to convert a std::set of strings to lowercase c++ using some combination of for_each, transform, iterators and lambda? 在 C++ 中将单个字符转换为小写 - tolower 返回一个整数 - Convert a single character to lowercase in C++ - tolower is returning an integer 如何将具有函数的C ++继承类转换为C样式? - How to convert C++ inherited classes with functions to C style? 使用C ++中的tolower()函数将字符串转换为小写字符 - convert strings to lowercase characters using tolower() function in c++ 导入ctype; 在C ++应用程序中嵌入python - Importing ctype; embedding python in C++ application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM