0001: Toggle String (conversion between uppercase and lowercase letters)

Problem

You have been given a String S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output.
Input Format
The first and only line of input contains the String S

Output Format
Print the resultant String on a single line.

Constraints
1≤|S|≤100 where |S| denotes the length of string S.

Solve

#include
int main(){
    char str[99];
    int i;
    scanf("%s",str);
    while(str[i]!='\0'){
        if (str[i]>='a'&&str[i]<='z')
            str[i] -= 32;
        else if(str[i]>='A'&&str[i]<='Z')
            str[i] += 32;
        i++;
    }
    printf("%s\n", str);
    return 0;
}

notes:

printf("%s\n", str); 比较
printf("%s\n", str[4]); 为错误
printf("%c\n", str); 错误
printf("%c\n", str[i]); 只输出一个字符

posted @ 2017-05-27 14:48  Mmsumz  阅读(234)  评论(0编辑  收藏  举报