The escape character in the string and the length of the string

Source: Internet
Author: User

Tips:

(1) When calculating the length of the string, the key is to recognize the escape character;

(2) An escape character is always preceded by a backslash, plus one other character. Therefore, you should pay special attention when you encounter the backslash!!!

Example:

1. String without escape character

such as: "abc!x=/", strlen the length of 7, but because the string at the end there is a Terminator ' \ ', so the string occupies 8 bytes, the length is 8.

2. String with escape character

(1) string "abc\n": where ' \ n ' is an escape character (line break), the length of a string can only be counted as one character, so the length of the string is 5 (not 6)

(2) The string "abc\n\\\" ": There are 4 escape characters: ' \ n ' (newline character), ' \ \ ' (backslash), ' \ ' (single quote), ' \ ' (double quotation mark), so the length of the string is 8 (instead of 12).

(3) string "ABC\0XYZ": There is an escape character ' \ s ', which is a string terminator, so when the length of the string is tested with the function strlen, the result should be 4 (not 8).

(4) The string "Abc\\0xy": where there is an escape character ' \ \ ' (backslash), so that the string "0xy" after the same calculation, so that the length of the string is 7 (instead of the second backslash and the subsequent 0 is combined into an escape character ' "", if so, The first backslash cannot be processed because an escape character is always made up of backslashes plus other characters, and a single backslash cannot be any valid character.

(5) If the string "Abc\\0xy" is changed to "Abc\\\0xy": There are two escape characters ' \ \ ' (backslash) and ' \ S ' (string terminator), then when the function strlen to test the length of the string, the result should be 5 (instead of 8).

(6) If the string "Abc\\\0xy" is changed to "Abc\\\061xy": Then there are two escape characters ' \ \ ' (backslash) and ' \061 ' (ASCII value equals 061 characters, that is, the numeric character ' 1 '), When a function strlen is used to test the length of the string, the result should be 7 (not 4 or 9). Therefore, when encountering the escape character ' + ', it is also necessary to see if there is a number behind it, if so, the following number (one to two bits) should be combined with the preceding ' + ' to count the length of the whole string as one character.

If you use printf ("Abc\\\061xy") and output, the output is: Abc\1xy

Note that using strlen to find the length of a string is not the end of the Terminator, but the number of bytes that the string occupies is a terminator.

Example one: [1995 level two C written selection question (26)]

Please select the output of the following statement ()

printf ("%d\n", strlen ("\t\" \065\xff\n "));

(A) 5 (B) (C) 8 (D) output not valid, no normal output

The correct answer is (A): The 5 characters contained in the string are: ' \ t ' (Jump Geff), ' \ ' (double quotes), ' \065 ' (ASCII value is octal 065, which is the decimal 51 character), ' \xff ' (ASCII value is hexadecimal ff, or decimal 255 character) , ' \ n ' (line break).

Example two: [1998 level two C written selection question (44)]

If you have the following program sections:

Char str[]= "ab\n\012\\\" ";

printf ("%d", strlen (str));

The output of the above program segment is

A) 3 B) 4 C) 6 D) 12

The correct answer is (C): The 6 characters contained in the string are: ' A ', ' B ', ' \ n ', ' \012 ', ' \ \ ' and ' \ '. If the question is to ask the length of the string, it is 7.

The escape character in the string and the length of the string

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.