c语言 字符串数组 将字符串中的小写字母转换为大写_m0
一种逃避了指针使用的做法~
任务描述
从键盘读入一个字符串,把字符串中的小写字母转换为大写字母,输出转换后的字符串和转换的字母个数。
测试说明
平台会对你编写的代码进行测试:
测试输入: abcABC123aB
预期输出: ABCABC123AB 4
- #include<stdio.h>
- #include<string.h>
- int tran(char s[]);
- int tran(char s[]) {
- scanf("%%s",s);
- //字符串数组可以直接以字符串格式%%s读入,这时用scanf不需要&
- int len=0;
- while (s[len]){
- len++;
- }
- //统计字符串数组长度的绝佳办法,先不定义长度
- //用一个while循环,当下标存在时计数
- //最终得到的累加值就是字符串数组的总长
- int cnt=0;
- int i;
- for(i=0;i<len;i++){
- if(s[i]>='a'&&s[i]<='z'){
- s[i]-=32;
- cnt++;
- }
- }
- return cnt;
- }
- int main( ){
- char s[20];
- int n=tran(s);
- //审题,n是转换次数,所以tran函数的返回值应该在转换的循环里计数,即cnt
- printf("%%s %%d
",s,n);
- return 0;
- }
推荐阅读