C language learning: 12. Character arrays and strings

1. Character type

A variable of type char occupies one byte of memory, with a value ranging from -128 to 127;

The char type can be understood as a single-byte integer type;

Variables of type char can be assigned and initialized like this

char a = 97;

char b = 'a';

2. Character array

Character arrays are essentially collections of single-byte integers, so character arrays can be assigned and initialized in this way.

char a[] = {97, 98, 99};

char b[] = {'h', 'e', 'l', 'l', 'o'};

Program example 1:

#include <stdio.h>

int main()
{
	char a[] = {97, 98, 99};
	int i = 0;
	
	for(i=0; i<sizeof(a);i++)
	{
		printf("%d\n", a[i]); //按照整型输出
	}
	
	for(i=0; i<sizeof(a);i++)
	{
		printf("%c\n", a[i]); //按照字符型输出
	}
}

3. String Facts

A common string uses double quotes to enclose some characters;

However, there is no data type like string in C language . Strings can only be simulated through character arrays.

The strings we often see are just literals and can only be used as constants.

#define KJ    "helloworld"
printf("%s\n", KJ);
printf("%s\n", "helloworld");

In my impression, the string variable seems to exist. The following program can "prove" it.

Program example 2:

#include <stdio.h>

int main()
{
	char arr[] = {'h','e','l','l','o','\0'};

	char arr1[] = "hello";

	printf("%s\n", arr);

	printf("sizeof(arr1) = %d\n", sizeof(arr1)); //数组arr1的大小应该为6,有一个隐形的\0
	printf("%s\n", arr1);
	return 0;
}

In fact, the so-called string variable is essentially a character array. The 0 element in the character array represents the end of a string. The elements in the character array are not necessarily the elements in the string. As shown in program example 2, there are a total of 6 elements in the arr array, but the string represented only has 5 characters, which should be \0. It is an element in the array, but when it represents a string, it means the end. symbol.

So it can be understood that if the character array does not have \0 elements, it is just a character array. If there are \0 elements in the character array, then this character array can also represent a string.

4. String operations

The C language does not have direct operations on strings. There is no direct focus on this. That is to say, there are indirect operations. Another tool package needs to be added. We have been using the stdio.h tool package before, and we will touch upon it again this time. A string.h toolkit serves string operations.

Commonly used string tools are as follows:

strlen(s);//获取字符串s的长度

strcpy(s1, s2);//将s2中的字符复制到s1

strcat(s1, s2);//将s2追加到s1后面

strcmp(s1, s2);//比较s1和s2是否相等,相等时返回结果0

Program Example 3: Get the length of a string

#include <stdio.h>
#include <string.h>

int main()
{
	char arr[10] = "hello";

	int size = sizeof(arr); //获取arr数组的大小,数组大小已经定义好了,就是10

	int len = strlen(arr); //获取arr字符串的长度,字符串的长度是5,只有5个字符


	printf("size = %d\n", size);

	printf("len = %d\n", len);

	return 0;
}

Output result:

size = 10
len = 5

Program Example 4: String Copy

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10] = "hello";

	char s2[] = "world!";

	int len = strlen(s1); //获取arr字符串的长度

	printf("s1 = %s\n", s1);
	printf("len = %d\n", len);

	strcpy(s1, s2);
	len = strlen(s1);
	printf("s1 = %s\n", s1);
	printf("len = %d\n", len);

	return 0;
}

Output result:

s1 = hello
len = 5
s1 = world!
len = 6

Program example 5: String concatenation

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[20] = "hello"; //这个数组空间要搞大一些,不然拼接的字符串放不下

	char s2[] = "world!";

	int len = strlen(s1); //获取arr字符串的长度

	printf("s1 = %s\n", s1);
	printf("len = %d\n", len);

	strcat(s1, s2);
	len = strlen(s1);
	printf("s1 = %s\n", s1);
	printf("len = %d\n", len);

	return 0;
}

Output result:

s1 = hello
len = 5
s1 = helloworld!
len = 11

Program Example 6: String Comparison

int main()
{
	char s1[20] = "hello";

	char s2[] = "world!";

	int len = strlen(s1); //获取arr字符串的长度

	printf("s1 = %s\n", s1);
	printf("len = %d\n", len);

	if (strcmp(s1, s2) == 0)
	{
		printf("两个字符串相等\n");
	}
	else
	{
		printf("两个字符串不相等\n");
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/m0_49968063/article/details/132938956