[C Language] 09-string

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

1. String Introduction

* In Java, a String can be stored as a String.

String s = "MJ";

The C language does not have the String type. In fact, strings are character sequences consisting of multiple characters. Therefore, in C language, we can use character arrays to store strings.

* A string can be considered as a special character array. to distinguish it from a common character array, an ending sign '\ 0' should be added at the end of the string '. '\ 0' is a character with an ASCII code value of 0. It is an empty operator, indicating nothing to do. Therefore, the character array is used to store strings. The ending sign '\ 0' should be included in the value assignment '.

* The storage of the string "mj" is as follows (assuming the character array char a [] is used for storage ):

Note that there is a '\ 0' at the end. If this end mark is not found, the character array is not stored as a string.

 

Ii. String Initialization
 1 char a[3] = {'m', 'j', '\0'}; 2  3 char b[3]; 4 b[0] = 'm'; 5 b[1] = 'j'; 6 b[2] = '\0'; 7  8 char c[3] = "mj"; 9 10 char d[] = "mj";11 12 char e[20] = "mj";

When we use the initialization method similar to 8th rows, the system will automatically add a \ 0 terminator to the end of the string.

 

3. String output

We can use two functions in stdio. h to output strings: printf and puts.

1. printf function

* This function has been used many times. The format character % s is used to output a string.

char a[3] = {'m', 'j', '\0'};printf("%s", a);

Output result: the \ 0 at the end is not output. It is only a NULL Character and only a flag of the end of the string.

 

* Speaking of this, some people may think: in this case, it seems that removing the final \ 0 has no effect, and the output results should be the same, all of which are "mj ".

We can try to remove the last \ 0 and then output it in the row:

char a[3] = {'m', 'j'};printf("%s", a);

Output result: it is the same as the output result with \ 0 added above.

Don't be happy too early. I can only say that you are lucky and lucky.

 

* Let's look at an example.

1 char a [3] = {'M', 'J', '\ 0 '}; // added the terminator \ 02 3 char B [] = {'I', 's'}; // if you forget to add the terminator \ 04 5 printf ("string: % s ", a); // output string a6 7 printf (" \ n "); // line feed 8 9 printf (" string B: % s ", B ); // output string B

As you can see, the ending character \ 0 is not added to the character array B of the fifth line. Therefore, B is not an authentic string.

According to your conjecture, the output of string B should be "is", but the output result is:, we can see that when we try to output B, a is also output.

To figure out why, first look at the memory addresses of a and B:

Printf ("a address: % x", a); printf ("\ n"); printf ("B address: % x", B );

Output result: We can analyze the memory storage of a and B as follows:

We can see that the memory addresses of array B and array a are continuous. Let's return to the output code of B:

Printf ("string B: % s", B); // output string B

% S indicates that a string is expected to be output. Therefore, the printf function outputs characters sequentially from the first address of B until \ 0, because \ 0 is the end mark of the string.

Therefore, if you want to create a string, add the terminator \ 0. Otherwise, the result will be serious and some junk data will be accessed.

 

2. puts Function
1 char a[] = "mj";2 puts(a);3 4 puts("lmj");

Check the Code in line 1. The puts function will output characters from the first address of a until \ 0.

Output result: it can be seen that the puts function will automatically wrap a string.

* The puts function can output only one string at a time. The printf function can output multiple strings at a time.

printf("%s - %s", "mj", "lmj");

 

Iv. String Input

Stdio. h has two functions that can be used to receive user input strings: scanf and gets.

1. scanf Function
char a[10];scanf("%s", a);

The scanf function stores user input characters from the first address of a. After the characters are saved, the system automatically adds an ending mark \ 0 to the end.

Note: Do not write it as scanf ("% s", & a). Because a already represents the address of the array, you do not need to add the & Address operator.

 

2. gets Function
char a[10];gets(a);

Like scanf, gets stores user-input characters starting from the first address of a. After the storage is complete, the system automatically adds an ending mark \ 0 at the end.

* Gets can only read one string at a time, while scanf can read multiple strings at a time.

* Gets can be used to read strings containing spaces and tabs until the carriage return occurs. scanf cannot be used to read spaces or tabs.

 

5. String Array 1. Introduction to String Array

* A one-dimensional character array stores a string, for example, a char name [20] = "mj"

* To store multiple strings, such as the names of all students in a class, a two-dimensional character array is required, char names [15] [20] can store the names of 15 students (assuming the name cannot exceed 20 characters)

* If you want to store the names of two students, you can use a three-dimensional character array char names [2] [15] [20].

 

2. String Array Initialization
char names[2][10] = { {'J','a','y','\0'}, {'J','i','m','\0'} };char names2[2][10] = { {"Jay"}, {"Jim"} };char names3[2][10] = { "Jay", "Jim" };

You can regard a string array as a one-dimensional array, and its element is a string. The string array names consists of the string "Jay" and string "Jim.

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.