4 条题解

  • 3
    @ 2023-11-21 21:10:35

    代码如下:

    #include <iostream>
    using namespace std;
    int main()
    {
        char a;
        cin >> a;
        if (a<='z' && a>='a')//判断a是不是小写字母
        {
            a-=32;//把a变成大写字母(大写字母都比小写字母小32)
        }
        else//否则
        {
            a+=32;//把a变成小写字母(小写字母都比大写字母大32)
        }
        cout << a;//输出
        return 0;//结束
    }
    
    • 1
      @ 2023-11-4 21:05:29
      #include <bits/stdc++.h> 
      using namespace std;
      int main()
      {
          char c;
          cin >> c;
          if(c >= 'a' and c <= 'z')
          {
              c -= 'a' - 'A';
          }
          else
          {
              c += 'a' - 'A';
          }
          cout << c;
          return 0;
      }
      
      • 1
        @ 2023-6-2 11:53:03
        #include <bits/stdc++.h>
        using namespace std;
        char a;
        int main()
        {
            cin >> a;
            if ('A' <= a && a <= 'Z')
            {
                cout << char(a + 32);
            }
            else
            {
                cout << char(a - 32);
            }
            return 0;
        }
        
        • 1
          @ 2023-1-25 15:12:36

          这里给大家推荐两个超实用的函数:

          toupper()

          函数原型:

          int toupper(int c)
          {
          	if ((c >= 'a') && (c <= 'z'))
          		return c + ('A' - 'a');
          	return c;
          }
          

          tolower()

          函数原型:

          int tolower(int c)
          {
          	if ((c >= 'A') && (c <= 'Z'))
          		return c + ('a' - 'A');
          	return c;
          }
          

          以上是两个转换大小写函数,分别为转换大写和转换小写,所以这道题就很简单了

          (注意,上面两个函数的返回值为int类型,题目要求输出字符,所以还得强制转换成char)

          ACcode:

          #include <iostream>
          #include <cstdio>
          
          using namespace std;
          
          int main()
          {
              char c;
              cin >> c;
              char newc;
              if (c >= 'A' && c <= 'Z')
                  newc = tolower(c);
              else
                  newc = toupper(c);
              cout << newc;
              return 0;
          }
          

          且慢!还有一种方法!

          我们可以利用ASCII码来做

          ASCII表上a为97,A为65,97-65 = 32正是ASCII码里大小写的差

          小写转大写只需-32,大写转小写只需+32

          这就是第二种方法:

          #include <iostream>
          #include <cstdio>
          
          using namespace std;
          
          int main()
          {
              char c;
              cin >> c;
              char newc;
              if (c >= 'A' && c <= 'Z')
                  newc = c + 32;
              else
                  newc = c - 32;
              cout << newc;
              return 0;
          }
          
          • 1

          信息

          ID
          968
          时间
          1000ms
          内存
          64MiB
          难度
          2
          标签
          递交数
          121
          已通过
          70
          上传者