将字符串的所有小写字母转换为大写字母--或奇数位

一、代码

 1 //将字符串中的小写字母转换为大写
 2 #include <iostream>
 3 using namespace std;
 4 //转换函数
 5 char *fun(char *str)
 6 {
 7     int i=0;
 8     int m=strlen(str);
 9     cout<<"字符串长度为"<<m<<endl;
10     cout<<"所有的小写字母转换后为:"<<endl;
11     while(str[i]!=0)
12     {
13         if(str[i]>='a'&&str[i]<='z')
14             str[i]=str[i]-'a'+'A';
15         cout<<str[i];
16         i++;
17     }
18     return 0;
19 }
20 /////////////////////////
21 void main()
22 {
23     cout<<"请输入字符串:"<<endl;
24     char a[20];
25     cin>>a;
26     fun(a);
27     cout<<endl;
28 }

二、运行

如果是将奇数位的小写字母转换为大写,只需把

1 if(str[i]>='a'&&str[i]<='z')

改为

1 if((i+1)%2!=0&&str[i]>='a'&&str[i]<='z')

结果演示:

原文地址:https://www.cnblogs.com/f59130/p/3330474.html