Three ways of converting letter size in C language

1. Use the ASCII value method (the difference between upper and lower case is 32)

method 1)

#include<stdio.h>
int main()
{
    char ch;
    printf("请输入一个字符:\n");
    scanf("%c", &ch);
    if (ch >= 'a' && ch <= 'z')
    {
        ch -= 32;
        printf("%c\n", ch);
    }
    else if (ch >= 'A' && ch <= 'Z')
    {
        ch += 32;
        printf("%c\n", ch);
    }
    else
    {
        printf("输入的不是大写或者小写字母\n");
    }
    return 0;
}


method (2)

#include<stdio.h>

void main()
{
    char a;
    printf("请输入一个字母:");
    scanf("%c", &a);

    if (a <= 91)         //对应ASCII表判断输入字母的ASCII值,大写字母A~Z的ASCII值为65~91
    {
        a = a + 32;   //字母a~z的ASCII值为97~123,则给该字符加32之后,他的ASCII值变为对应的小写字母的ASCII值
        printf("该子母的小写形式为:%c\n", a);

    }
    else
    {
        a = a - 32;   //同大写变小写的ASCII值的转换
        printf("该子母的大写形式为:%c\n", a);
    }
}

 

 Note: The above two code ideas are implemented , but the implementation process of the intermediate code is slightly different.

 

2. Implementation of bitwise XOR method

              (1) Considering that the lower four digits are exactly the same, only by exchanging the sixth digit of the upper four digits, the upper and lower case can be swapped.
              (2) Exchange a certain digit of the upper four digits without changing the lower four digits. Bitwise XOR in operation ^ For details about bitwise XOR, see Bitwise XOR

              (3) Based on the knowledge of bitwise XOR, we can find a number so that after any eight-bit binary number is XORed with it, the sixth bit 01 of the upper four bits is reversed, and the lower four bits remain unchanged.

Looking back at our goal, if we change 'A' to 'a', we need to change 01000001 to 01100001, that is, change the sixth digit 0 to 1 

We found the number! Its binary value is 00100000 and its decimal value is 32

01000001^00100000=01100001

#include<stdio.h>
int main()
{
    int i = 0;
    char arr[100];
    gets_s(arr);
    for (i = 0; arr[i] != '\0'; i++)
    {
        if ((arr[i] >= 'A') && (arr[i] <= 'Z') || (arr[i] >= 'a') && (arr[i] <= 'z'))
        {
            arr[i] ^= 32;
        }
    }
    printf("%s",arr);
    return 0;
}

3. String case conversion function

  1. toupper function
    toupper is a lowercase to uppercase function
  2. The tolower function
    tolower is an uppercase to lowercase function

Function specific usage:

 

The code example is as follows: 

#include <iostream>
#include <ctype.h>// toupper  tolower
#include <cstring>
using namespace std;

int main()
{
    char a[100];
    int n, i;
    cin >> a;
    n = strlen(a);
    for (i = 0; i < n; i++)
    {
        a[i] = toupper(a[i]);//小写转大写
    }
    cout << a << endl;
    for (i = 0; i < n; i++)
    {
        a[i] = tolower(a[i]);//大写转小写字母(只能是对字母有效)
    }
    cout << a << endl;
    return 0;
}

 

Editor's note: The above-mentioned various methods of writing code for this topic are welcome to collect, learn from and forward;

               The above code is for reference only, if you have any questions, you are welcome to criticize and correct in the message area;

               All rights reserved, reprints must be investigated, any similarity is purely coincidental, please indicate the source for reprinting.

               By CRH380AJ2808 2022.06.09
—————————————————
Copyright Notice: This article is the original article of CSDN blogger "CRH380AJ2808", following the CC 4.0 BY-SA copyright agreement, please attach the original text for reprinting Source link and this statement.
Original link: https://blog.csdn.net/JH13thpig/article/details/125194827

Guess you like

Origin blog.csdn.net/JH13thpig/article/details/125195046