The relationship between a character array and a string in C language __c language

Source: Internet
Author: User
Tags lowercase strcmp word wrap

End flag of a string

1, most of the time we can see the relevant content is: using arrays to store strings, that is, we often use the sizeof () and this function, and this function is only to find the current maximum capacity of the array, rather than the actual contents of the array, we generally need to use the ' \ 0 ' to represent the end of the string and not other content that affects the operation of the corresponding array, but is automatically added outside the system. This is the problem with the operating system, so we can use ' yes ' to determine the end of the string.
(So we use a while to loop the output of the string, etc., the length of the string is less important)
' I ' An empty operator, and the ASCII code is 0, which is not displayed, and there is nothing to do; that is: no additional action is added or a valid character is added. Just to play a distinguishing sign.


2, constants in the string
eg:printf ("How do I do? \ n");
Think about it, how the system knows where the string ends ... Maybe we all take it for granted that the system tells us.
Actually: when stored in memory, the system automatically adds "\ n" to the end of the string.
The principle of printf execution is to check whether the character is ' "" before executing a character, and to stop the output if it is encountered.


Initialization of strings:
Similar to our general data for initialization:
Mode 1 char c[]={' I ', ', a ', ' m ', ', h ', ' A ', ' P ', ' P ', ' Y ', '};//' is assigned one by one


By understanding the constants of a string:
To assign a value by means of a string;
Mode 2) Char c[]={"I am Happy"};


Mode 1 The length of the string is 11, mode 2) The length of the string is 10


So: if,
Char c[10] = {"A"};
The first five are elements, the sixth is ' yes ', and the following 4 are also set to null characters.


Description: The string does not require the last character to be ' a ', or even contain ' the '. The system will automatically increase if you need to ' go '.


In order to deal with the constant nature of the method, it is often thought that the addition of a ' "' is in the back of the data.
Eg:char c[6]={' C ', ' h ', ' I ', ' n ', ' a ', ' I '};
This makes it easy to reference strings in a character array.

Summarize:
We should use a uniform paradigm, whether the array is assigned a value by its corresponding checkmark, or by the way the string constants are assigned.
That is, the way the array is, we should assign a ' value ' as the end sign, and the string constant system will automatically increase.

Ii. input and output of character arrays
Two ways:
1, one character input output, the format character is "%c", circular output.
2, the entire string output together. The format character is "%s", which indicates that the output is a string
Char c[] = {"A"};
Prinft ("%s", c);
Output, the output is stopped when the '%c ' is encountered. The output is the result of the
Precautions:
1 The output character does not contain ' the ', because this is the problem of the operating system and the language, not the data itself.
2 "%s" the output item in the function is the character array name.
3 If the length of the array is greater than the actual length of the string, or encountered ' "" to stop the output.
Char c[10]={"a"; The same output: the 10 characters in the output
4 If there are more than 1 symbols in the array, the output will stop when it encounters the first '.
5 can use scanf ("%s", c); Enter a string

Char str1[5],str2[5],str3[5];
scanf ("%s,%s,%s", STR1,STR2,STR3);
Enter data:
How are?
The condition of the element storage:
How\0\0
Are\0\0
You?\0
When input is entered, the space indicates the end of the input.

if replaced by;
Char str[13];
scanf ("%s", str);
Keyboard input:
How are?
The output will be:
How
Principle:
How\0\0 ... (all of them are in the back)
Reason: Because the system takes a space symbol as a separator between input strings.

Three, the string processing function
1, puts (character array)
Function: Outputs a string (a series of characters that ends with ' and ') to the terminal.
eg
Char str[]={"Bei Jing"};
Puts (str);


Char str[]={"\nbei Jing"};
printf ("%s", str); There's no word wrap at this end.
Puts (str); This is the end of the wrap line
Because the output of strings often uses the printf () function, the puts () function is used very little.

2, gets (character array)
Function: Enter a string from the terminal to the character array, and get a function value.
The function value is the starting address of the character array.
eg
Gets (str)


#include <stdio.h>
#include <stdlib.h>
int main ()
{
Char str[100];
Char *p;
p = gets (str);
The output string "Hello" in the terminal, which sends the Hello string to array str,
And note that the number of sending character arrays is 6 instead of 5,
Because there is also a end identifier


Puts (p); Output string and wrap line
printf ("%s\n", p); Output string
printf ("%p\n", p); Output first Address
(General: The purpose of the Gets () function is to enter a string into a character array, and two not to care about its function value)
return 0;
}


Precautions:
Puts ()
Gets ()
can only output or input a string, can not input, output multiple strings.
The following is the wrong format:
Gets (STR1,STR2);
Puts (STR1,STR2);

3, strcat (character array 1, character array 2) full name (string catenate character connection)
Links the string in character array 2 to the end of the character array 1, and the returned function value is the first address of the character 1.
That is, the result is stored in character array 1.
eg
#include <stdio.h>
#include <stdlib.h>
int main ()
{
Char str1[40] = {"I am student"};
Char str2[] = {"You are very good"};
printf ("%s", strcat (STR1,STR2));
return 0;
}
The results of the output will be: I am student you are very good

Precautions:
1, that is, the array 1 should be large enough to accommodate the string added later.
If the above changes the str1 to: Char str1[]={"..."}, that is, the error is displayed after the data has been added without providing enough controls.
2, two strings are followed by a ' str1 ' character, connected to the later, there will be no, after the link after the most of the "" "character.


4, strcpy, strncpy function
(string copy) string assignment (string number copy)
strcpy (character array 1, string 2)
Function: Copy the string 2 into the character array 1.
eg
Char str1[10]= ', str2[]={""
Description
1 character array 1 must be defined large enough to hold the string 2
2 The character array 1 must be written in the form of an array name, and "String 2" can be either a character array name or a string constant.
eg:strcpy (STR1, "the");
3 If the STR1 array is not assigned a value before the assignment, the contents of the str1 will be unknown. Copying the string in the str2 with the ' "The following ' is copied to the character array 1, which means that 6 characters are copied, and the following 4 characters keep the original content.
4 cannot give a string constant or an array of characters directly to an array of characters.
Eg: The following is an illegal way of saying:
STR1 = "the";
STR1 = str2;
Reason:
Assignment Symbol "=", the assignment statement can only assign a "character" to a "character variable";
A string can only assign a string to another character array using the strcpy function.
&& is also the C language string assignment can only use strcpy and so on copy functions.
5) The strncpy function copies the first n characters in string 2 to the character array 1
As follows:
strncpy (str1,str2,2);


5. strcmp function
strcmp (String 1, String 2) Full name: string Campare strings Comparison
Compare String 1 and String 2
string comparison rules:
Two strings from left to right compared to characters (follow ASCII code value comparison size), know the characters are different or ' the ' when the cutoff, to determine the size.
STR1&GT;STR2, the function value is a positive number
STR1=STR2, the function value is 0
STR1&LT;STR2, the function value is a negative number


6, strlen (characters array) full name: string length (length of string)
The value of the function is the actual length of the string and does not contain a ' yes '.
Even though we have ' the ' in the definition of an array, it's only added for the sake of unified processing.
#include <stdio.h>
#include <stdlib.h>
Standard library Hansu in #include <string.h> \\c
int main ()
{
Char str3[]={' d ', ' 2 ', ' n '};
Char str4[]={' d ', ' 2 '};

printf ("%d \ n", strlen (STR3));

printf ("%d \ n", strlen (STR4));
printf ("%d \ n", strcmp (STR3,STR4));

printf ("%s \ n", STR3);
printf ("%s \ n", STR4);
return 0;
}
1, note that the C language standard library has string.h header file, but there is no string of this class of argument,
2, C language in the String.h header file is stored in the strcpy and so on functions
3, string is a common noun, that is, the meaning of the string, in the C language is always stored some of the data on the char type operation,
As already mentioned in (2) above, you know that there are string classes in C + + that you can define the corresponding objects.
4, Strlen calculated is the length of the effective string,



5. The result of the above procedure is:

Why is there such a problem?

Because we are initialized directly through the character array, not automatically at the end of the "" ", in memory or automatically stored in memory, so there is no ' good ' as a terminator, so there will be the above error,

If we use the function scanf () and the Gets () function, the compiler will automatically add '.

If you use the Word constants quantity to express, the above already mentioned, will automatically add ' the ', so special is the character array.

At the same time, the previous reference to the end of the character array with ' and no ' is not much change. The lengths are equal. may be different versions.



7, STRLWR (string) full name; string lowercase strings lowercase
Programming characters in a string to lowercase


8, STRUPR (String) Full name: string uppercase
To programmatically capitalize characters in a 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.