C language-String Length Calculation Method

Source: Internet
Author: User

Tip: (1) when calculating the length of a string, the key is to recognize the escape character. (2) an escape character always starts with a backslash and is composed of another character. Therefore, pay special attention to the backslash !!!

1. A string without escape characters

For example, "ABC! X =/", whose length is 7

 

2. character strings with escape characters

(1) string "ABC \ n": '\ n' is an escape character (line break). Only one character can be used to calculate the length of a string, therefore, the length of the string is 4 instead of 5)

(2) string "ABC \ n \ '\" ": contains four escape characters:' \ n' (line break) and '\' (backslash), '\ ''(single quotes),' \" '(double quotes), so the length of this string is 7 (rather than 11 ).

(3) string "ABC \ 0xyz": There is an escape character '\ 0', which is the string Terminator. Therefore, when the strlen function is used to test the length of the string, the result should be 3 (instead of 7 ).

(4) string "ABC \ 0xy": There is an escape character '\' (backslash). In this way, the following string "0xy" is still calculated, the length of the string is 7 (instead of combining the second backslash with the subsequent 0 as an escape character '\ 0'. Otherwise, the first backslash cannot be processed, because an escape character is always composed of a backslash and other characters, a single backslash cannot be any valid character ).

(5) If you change the string "ABC \ 0xy" to "ABC \ 0xy", there are two escape characters '\' (backslash) and '\ 0' (string Terminator). When the strlen function is used to test the length of the string, the result is 4 (instead of 7 ).

(6) If you change the string "ABC \ 0xy" to "ABC \ 061xy", there are two escape characters '\' (backslash) and '\ 061' (ASCII code value is equal to 061 characters, that is, the number character '1'). When the strlen function is used to test the length of the string, the result should be 7 (instead of 4 or 9 ). Therefore, when you encounter an escape character '\ 0', you must check whether there are any numbers behind it. If so, you must add one to two numbers) it is combined with '\ 0' in front of it as a character to calculate the length of the entire string.

If printf ("ABC \ 061xy") is used, the output result is: ABC \ 1xy.

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.