C Primer Plus (11)

Source: Internet
Author: User

11.1 string representation and string I/O

A string is a char array ending with an empty character (\ 0.

11.1.1 Initialization

I. string constants

A String constant, also known as string text, refers to any character in a pair of double quotation marks. The characters in double quotation marks are added with the ending sign \ 0 automatically provided by the compiler, and are stored in memory as a string.
If there is no interval in the string text or the spacing is a space character, ansi c concatenates it.
For example, char get [50] = "How are" "you"; equivalent to char get [50] = "How are you ";
To use double quotation marks in a string, you can add a backslash before the double quotation marks.
A String constant belongs to a static storage class, that is, it is called multiple times in a function. This string is only stored in one copy throughout the entire process of the program.

Ii. String Array and initialization

When the array size is specified, make sure that the number of array elements is at least 1 longer than the string length. All unused elements are automatically initialized to 0. (\ 0 instead of 0)
The character array name is also the address of the first element of the array.

3. Differences between arrays and pointers

Char heart [] = "I love Tillie! ";
Char * head = "I love MIllie! ";
The main difference is that the array name heart is a constant, and the pointer head is a variable.
1. Both can use array symbols.
2. You can use pointer addition for both methods.
3. Only the pointer can use the incremental operator.
The element of the array is a variable, but the array name is not a variable.
You can add const when declaring the pointer to prevent changing the string content.

Const char * mytal1 [5] char mytal2 [5] [81]
The former is an array pointing to a char pointer, and the latter is an array of a char array.
The former stores 5 addresses, while the latter stores 5 complete character arrays.

11.2 string Input

First declare the array size: for example, char name [81];

Gets () reads all the characters before the linefeed and adds an empty character after these characters.
Gets () uses an address to assign a string to it, and it returns a pointer value pointing to char. If an error occurs or the end of the file is encountered, it returns an empty address. (NULL)



The fgets () function improves this problem by allowing you to specify the maximum number of characters to read.
1. It requires the second parameter to read the maximum number of characters in the title. If the value of this parameter is n, it reads a maximum of n-1 characters or reads a line break, which is determined by the conditions that are met first.
2. If it reads a linefeed, it stores it in a string instead of discarding it like gets.
3. It also requires the third parameter to indicate which file to read. When reading data from the keyboard, you can use stdin as this parameter.

You can also use the scanf () function with the % s format to read a string. Scanf () is more based on obtaining words than strings.

11.3 string output

The puts () function is easy to use. You only need to provide the address of the string parameter. When puts () is a real string, A linefeed is automatically added to it.

The fputs () function is a file-oriented version of gets ().
1. fputs () requires the second parameter to describe the file to be written. You can use stdout as the parameter for output display.
2. fputs () does not automatically add line breaks for the output.

You can also use the printf () function to output strings.

11.4 custom string Input/Output Functions

# Include <stdio. h>
Void put1 (const char * string)
{
While (* string! = '\ 0 ')
Putchar (* string ++ );
}
If you are familiar with it, you can set the while (* string! = '\ 0') to while (* string)

11.5 string functions

I. strlen () function

Use the strlen () function to obtain the string length.

Ii. strcat () Functions

The strcat () function accepts two string parameters. It adds a copy of the second string to the end of the first string, making the first string a new composite string, the second string has not changed.
The strcat () function is of the char * type. This function returns the value of its first parameter.

Iii. strncat () function

The strcat () function does not check whether the first array can accommodate the second string. The strncat () function requires another parameter to specify the maximum number of characters that can be added, for example, strncat (bugs, addon, 13) until it is added to 13 characters or a null character is encountered.

Iv. strcmp () Functions

The strcmp () function is used to compare the content of a string. It compares the content before the first null character. Therefore, it can be used to compare strings stored in arrays of different sizes.
If the first string is in the alphabetic order prior to the second string, strcmp () returns a negative number. If it is the same, it returns 0. If it is later, a positive number is returned.
Strcmp () is compared according to the machine encoding sequence.

V. strncmp () function

When strncmp () compares strings, it always finds different corresponding characters. You can also compare the number of characters specified by the third parameter.

6. strcpy () andStrncpy ()Function

Strcpy () accepts two string pointer parameters. Copy the content of the second string to the address specified by the first pointer.

The strcpy () function has two other useful attributes. First, it is of the char * type, and it returns the value of the first parameter, that is, the address of a character. Second, the first parameter does not need to point to the beginning of the array, so that only part of the array can be copied.
Strncpy () requires the third parameter to specify the maximum number of characters that can be copied.

7. sprintf () function

The first parameter of the sprintf () function is the address of the target string. The remaining parameters are the same as those of printf. Example: sprintf (formal, "% s, %-19 s: $ % 6.2f \ n", last, first, prize );
Format the input to the standard format and store it in the string formal.

11.6 command line parameters

Compile this program into an executable file repeat. Command Line C> repeat Resistance is futile output is as follows
The command has 3 arguments
1: Resistance
2: is
3: futile
The first parameter is the number of strings in the command line, including the command. The second parameter is a pointer array pointing to a string. (Argc: argument count; argv: argument value)
If repeat "I am hungry" now is used, argc = 3;

11.7 convert a string to a number

The strol (), stroul (), and strtodd () functions convert a string to a long value. The prototype is as follows:
Long strol (const char * nptr, char ** endptr, int base );

If the base number is 10, the string "10" is converted to 10. If the base number is 16, it is converted to 16. The first conversion ends when an empty character is encountered, so that the end point to an empty character.
For the second input string, the end is the address of the 'A' character. Therefore, the output end displays the string "atom ", the output * end indicates the ASCII code of the 'A' character. However, if the base value is 16, the 'A' character will be recognized as a valid hexadecimal number, and the function will convert the hexadecimal number 10a to the decimal 266.

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.