PAT 06-2 字符串字母大小写转换

没什么好说的,记得使用ctype.h就好了,谭浩强那本书就介绍了,再不使用就太对不起他老人家了;有一点小小的地方需要注意一下,&&的优先级比=号高,所以getchar()两边没有括号的话呢,你就会看到...我试着用输出重定向(freopen())去获得这个字符,好吧,什么都没有,这叫什么来着,非打印字符,说多了,题设要求及代码实现如下

/*
    Name: 
    Copyright: 
    Author: 
    Date: 31/03/15 20:20
    Description: 
输入一个以#结束的字符串,本题要求将小写字母全部转换成大写字母,把大写字母全部转换成小写字母,其它字符不变。

输入格式:

输入在一行中给出一个长度不超过40的、以#结束的非空字符串。

输出格式:

在一行中按照要求输出转换后的字符串。

输入样例:
Hello World! 123#
输出样例:
hELLO wORLD! 123
*/
#include <stdio.h>
#include <ctype.h>

int main()
{
//    freopen("in.txt", "r", stdin); // for text
//    freopen("out.txt", "w", stdout); // for fun
    
    char ch;
    
//    while(ch = getchar()) // for debug
//        putchar(1); // for fun

//    ch = getchar(); // for replace
    while((ch = getchar()) && ch != '#')
    {
        if(isalpha(ch))
        {
            if(isupper(ch))
                ch = tolower(ch);
            else
                ch = toupper(ch);
        }
        putchar(ch);
//        ch = getchar(); for replace
    }
    
//    fclose(stdin); // for test
    fclose(stdout);
    
    return 0;
}
原文地址:https://www.cnblogs.com/qingkai/p/4382004.html