"C-language summary" string

Source: Internet
Author: User

Storage of strings

The string is stored in a character array, and the last one must be
, the compiler considers this character array as a pointer to the char* type.

For example: printf("abc") //传递的时abc的地址,并且是a的地址
Operation:

char"abc";
char"abc"[1];
String variables
charstr[8"abcdefg";//会把这几个字符复制到这个字符数组中;

He is stored in the following ways:

If the string is not 7, the remaining units will be filled in.

If more than 7 will not consider this array as a string, the string must be followed by A;

charstr"abcdeg";//编译器会自动计算长度,str是首字符的地址char *str"abcdef";//str是一个指针指向abcdef首字符的地址,并且str一定要指向一个存在的字符串
Read and write String
printf("string is %s"str);//puts(str);scanf("%s"str);   //首先会跳过空白字符,然后读取,再次遇见空白字符停止读取gets(str);//遇见换行符才会停止
C Language String Library

The C language string library must contain # include

charstr[10];str"abc";//错误,不能使用=号运算符把字符串复制到字符数组中

String copy function strcpy function

char"hello world!";char str2[10];strcpy(str2, str1);

string concatenation function Strcat function

strcat(str1, "abc");//这里的str1要有足够控件容下abc

string comparison function strcmp function

strcmp(str1, str2);//相等返回0,str1>str2就返回大于0的数

function Strlen function for finding string length

intlen = strlen(str1);
Best way to store strings

The best way to store a string is a two-dimensional array;

charstring[][3] = {"hello""nihao"};char *string[] = {"hello","world","I","love","you"};

"C-language summary" 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.