[英]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.