Talk C Chestnut Bar (166th: C Language Instance--uppercase and lowercase characters conversion)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about examples of constants in C, this time we say the example is: uppercase and lowercase character conversion . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, English characters are often used in programs, and it is sometimes necessary to convert English characters to uppercase and lowercase. How do I convert it? Let's take a look at the specific conversion method.

Everyone knows that each character has a corresponding ASIIC code, if the ASIIC code of a character plus a value will become another character of the ASIIC code. For example, the ASIIC code of the character a plus 1 is a character B's ASIIC code. Based on this principle, we can add a value to the ASIIC code of the lowercase character and convert it to the ASIIC code of the uppercase form. But there is one more question, that is, how much is the added value? In fact, we can find the law from the ASIIC Code table, the value is fixed, and we can calculate it, the calculation method is: "a-A" .

In addition, in the ASIIC Code table, the ASIIC code of lowercase characters is larger than the uppercase characters, so it is necessary to subtract a value when converting the lowercase character to a size character. Conversely, when you convert uppercase characters to lowercase characters, you add a value. Here's a detailed code to illustrate:

    char‘a‘;                     //定义一个小写字符a    char val_upper = val_lower - (‘a‘-‘A‘);   //把小写字符a转换为大写字符A    printf("the lower is %c ,the upper is %c \n",val_lower,val_upper);

As you can see from the code, the way to convert lowercase characters to uppercase characters is the one we just introduced. At this point, the program outputs the following results:

thelowera ,theupper

From the run result, you can see lowercase characters converted to uppercase characters.

Sometimes you can define a macro for conversion convenience, the following is the definition of a macro:

    #define TOUPPER(c) ((c)-(‘a‘-‘A‘))  //把小写字符转换为大写字符    #define TOLOWER(c) ((c)+(‘a‘-‘A‘))  //把大写字符转换为小写字符    printf("the lower is %c ,the upper is %c \n",val_lower,TOUPPER(val_lower));    printf("the upper is %c ,the lower is %c \n",val_upper,TOLOWER(val_upper));

Here is the result of the program running at this time:

thelowera ,theuppertheupper is A ,thelowera

In fact, the C standard library also provides conversion functions for uppercase and lowercase characters, which are located <ctype.h> in the file and whose function prototypes are as follows:

   inttoupper(int c);   //把小写字符转换为大写字符   inttolower(int c);   //把大写字符转换为小写字符

The use of these two functions is similar to the use of macros in our code just now, so we do not specify them in detail. However, they are well-portable.

In the standard library, these two functions are implemented by a translation table that contains content other than the ASIIC code. Our conversion method is based on the character ASIIC code representation, so if we use other means to represent the character, then the macro we define will be invalidated. For this reason, we recommend that you use the uppercase and lowercase character conversion functions provided by the standard library.

Crossing, here do not write code, complete code put in my resources, you can click here to download the use.

The following is the results of the operation of the program, please refer to:

Initvalue: the LowerIsa, the UpperIs A the LowerIsa, the UpperIs A the UpperIs A, the LowerIsa  the LowerIsa, the UpperIs A the UpperIs A, the LowerIsa 

Crossing, for example of uppercase and lowercase character conversions, let's talk about this. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (166th: C Language Instance--uppercase and lowercase characters conversion)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.