程序设计C 实验五 题目五 一维动态数组的应用(0293)

Time limit(ms): 1000
Memory limit(kb): 65535
Submission: 13295
Accepted: 5193
Accepted

输入一数字,用其控制一个数组的长度,而后将数组中的小写字母转换成大写字母,大写字母转换成小写字母,其余字符不变。

Description

连续输入多个测试数据: 输入的第一行为一个整数n,接下来有一个由n个字符组成的字符串。 输入0控制程序结束。

Input

每一行输出一组测试数据对应的结果,依次输出转换后的字符串。

Output
1
2
3
4
5
6
10
ASXZXCaz-+
10
azsx90AZKJ
0
Sample Input
1
2
3
asxzxcAZ-+
AZSX90azkj

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main() {
    char *ptr_s,*ptr_q,c;
    int n;
    while(scanf("%d",&n) == 1 && n != 0) {
        getchar();
        ptr_s = (char *)malloc(n + 1);    //动态创建数组
//      gets(ptr_s);                      //弃用gets()
        fgets(ptr_s,n+1,stdin); 
        ptr_q = ptr_s;
        while((c = *ptr_q) != '\0') {
            printf("%c",(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? c^0x20 :c); //采用异或运算可提高运算效率
            ptr_q++;
        }
        printf("\n");
        free(ptr_s);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Aimee_ice/article/details/78284218