Loading

字符数组与字符串


返回 我的技术栈(Technology Stack)



字符数组与字符串区别

  • C语言中没有字符串这种数据类型,可以通过char的数组来替代;
  • 字符串一定是一个char的数组,但char的数组未必是字符串;
  • 数字0(和字符‘\0’等价)结尾的char数组就是一个字符串,但如果char数组没有以数字0结尾,那么就不是一个字符串,只是普通字符数组,所以字符串是一种特殊的char的数组。
#include<stdio.h>

int main(void) 
{
	//定义字符数组   【可以存储多个】
	//char arr[5] = { "h","e","l","l","o" };   //必须要用但引用,否则报错,它会认为你是 const char *  类型的,这个类型不是单个字符
	char arr[5] = { 'h','e','l','l','o'};
	for (int  i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
	{
		printf("%c",arr[i]);   //输出:hello
	}

	//【 %s 要求有\0   没有\0会一直往后找,arr[5]里面没有0或者\0,所以会一直找,就会导致乱码,如果arr[6]或者大于6,那么是可以的,因为数组里面没有赋值的会默认填0  【数值 0 等同于 \0】】
	printf("%s", arr);   //hello烫烫烫虘﹖\(?s ?      
	printf("%5s", arr);   //hello    可以对输出进行限定



	// 字符  【只能存储一个】
	//char ch = "a";  //必须要用但引用,否则报错,它会认为你是 const char *  类型的,这个类型不是单个字符
	char ch = 'a';


	//字符串   字符串的结束标志为 \0,所以下面的长度是6 【字符串是字符数组的一个特例,一个有\0的字符数组,我们也可以认为是字符串】
	char* arr1 = "hello";    //长度为6
	char arr1[]= "hello";    //长度为6

	char arr1[] = { 'h','e','l','l','o','\0'};   //长度为6
	printf("%s", arr1);  //输出:hello      会一直找,直到 \0

	char arr2[] = { 'h','e','l','l','o','\0' ,'!'};   //长度为6
	printf("%s", arr2);  //输出:hello      会一直找,直到 \0
}
#include <stdio.h>

int main()
{
	char c1[] = { 'c', ' ', 'p', 'r', 'o', 'g' }; //普通字符数组
	printf("c1 = %s\n", c1); //乱码,因为没有’\0’结束符

	//以‘\0’(‘\0’就是数字0)结尾的字符数组是字符串
	char c2[] = { 'c', ' ', 'p', 'r', 'o', 'g', '\0' };
	printf("c2 = %s\n", c2);

	//字符串处理以‘\0’(数字0)作为结束符,后面的'h', 'l', 'l', 'e', 'o'不会输出
	char c3[] = { 'c', ' ', 'p', 'r', 'o', 'g', '\0', 'h', 'l', 'l', 'e', 'o', '\0' };
	printf("c3 = %s\n", c3);

	return 0;
}

输出:
c1 = c prog烫烫烫?I髄
c2 = c prog
c3 = c prog

字符串的初始化

#include <stdio.h>

// C语言没有字符串类型,通过字符数组模拟
// C语言字符串,以字符‘\0’, 数字0
int main()
{
	////不指定长度, 没有0结束符,有多少个元素就有多长
	//char buf[] = { 'a', 'b', 'c' };
	//printf("buf = %s\n", buf);	//乱码    因为没有没有0或\0结束符

	////指定长度,后面没有赋值的元素,自动补0
	//char buf2[100] = { 'a', 'b', 'c' };
	//char buf[1000] = { “hello” };
	//printf("buf2 = %s\n", buf2);

	////所有元素赋值为0
	//char buf3[100] = { 0 };

	////char buf4[2] = { '1', '2', '3' };//数组越界

	char buf5[50] = { '1', 'a', 'b', '0', '7' };
	printf("buf5 = %s\n", buf5);    // 输出:buf5 = 1ab07

	char buf6[50] = { '1', 'a', 'b', 0, '7' };
	printf("buf6 = %s\n", buf6);   //输出:buf6 = 1ab

	char buf7[50] = { '1', 'a', 'b', '\0', '7' };
	printf("buf7 = %s\n", buf7);  // 输出:buf7 = 1ab

	//使用字符串初始化,编译器自动在后面补0,常用
	char buf8[] = "agjdslgjlsdjg";

	//'\0'后面最好不要连着数字,有可能几个数字连起来刚好是一个转义字符
	//'\ddd'八进制字义字符,'\xdd'十六进制转移字符
	// \012相当于\n
	char str[] = "\012abc";
	printf("str == %s\n", str);  
	//输出:str ==
	//			abc
	return 0;
}

字符串的输入输出

由于字符串采用了'\0'标志,字符串的输入输出将变得简单方便。

#include <stdio.h>

int main()
{
	char str[100];

	printf("input string1 : \n");
	scanf("%s",str);     //scanf(“%s”,str)默认以空格分隔
	printf("output:%s\n", str);

	return 0;
}

gets()

#include <stdio.h>
char *gets(char *s);
功能:从标准输入读入字符,并保存到s指定的内存空间,直到出现换行符或读到文件结尾为止。
参数:
	s:字符串首地址
返回值:
	成功:读入的字符串
	失败:NULL
	
使用:
	char str[100];
	printf("请输入str: ");
	gets(str);
	printf("str = %s\n", str);

gets(str)与scanf(“%s”,str)的区别:

  • gets(str)允许输入的字符串含有空格
  • scanf(“%s”,str)不允许含有空格

注意:由于scanf()和gets()无法知道字符串s大小,必须遇到换行符或读到文件结尾为止才接收输入,因此容易导致字符数组越界(缓冲区溢出)的情况。

fgets()

#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:从stream指定的文件内读入字符,保存到s所指定的内存空间,直到出现换行字符、读到文件结尾或是已读了size - 1个字符为止,最后会自动加上字符 '\0' 作为字符串结束。
参数:
	s:字符串
	size:指定最大读取字符串的长度(size - 1)
	stream:文件指针,如果读键盘输入的字符串,固定写为stdin
返回值:
	成功:成功读取的字符串
	读到文件尾或出错: NULL

使用:
	char str[100];
	printf("请输入str: ");
	fgets(str, sizeof(str), stdin);
	printf("str = \"%s\"\n", str);

fgets()在读取一个用户通过键盘输入的字符串的时候,同时把用户输入的回车也做为字符串的一部分通过scanf和gets输入一个字符串的时候,不包含结尾的“\n”,但通过fgets结尾多了“\n”fgets()函数是安全的,不存在缓冲区溢出的问题

puts()

#include <stdio.h>
int puts(const char *s);
功能:标准设备输出s字符串,在输出完成后自动输出一个'\n'。
参数:
	s:字符串首地址
返回值:
	成功:非负数
	失败:-1
	
使用:
#include <stdio.h>

int main()
{
	printf("hello world");
	puts("hello world");

	return 0;
}

fputs()

#include <stdio.h>
int fputs(const char * str, FILE * stream);
功能:将str所指定的字符串写入到stream指定的文件中, 字符串结束符 '\0'  不写入文件。 
参数:
	str:字符串
	stream:文件指针,如果把字符串输出到屏幕,固定写为stdout
返回值:
	成功:0
	失败:-1

使用:
	printf("hello world");
	puts("hello world");
	fputs("hello world", stdout);

fputs()是puts()的文件操作版本,但fputs()不会自动输出一个'\n'。

strlen()

#include <string.h>
size_t strlen(const char *s);
功能:计算指定指定字符串s的长度,不包含字符串结束符‘\0’
参数:
s:字符串首地址
返回值:字符串s的长度,size_t为unsigned int类型

使用:
	char str[] = "abc\0defg";
	int n = strlen(str);
	printf("n = %d\n", n);
#include<stdio.h>
#include<string.h>

int main(void) 
{
	char ch1[100] = "hello word";
	printf("数组大小:%d", sizeof(ch1));  //数组大小:100
	printf("字符串长度:%d", strlen(ch1)); //字符串长度:10

	char ch2[] = "hello word";
	printf("数组大小:%d", sizeof(ch2));  //数组大小:11          因为字符串默认是以 \0 结尾的
	printf("字符串长度:%d", strlen(ch2));  //字符串长度:10
}

字符串追加

#include <stdio.h>

int main()
{
	char str1[] = "abcdef";
	char str2[] = "123456";
	char dst[100];

	int i = 0;
	while (str1[i] != 0)
	{
		dst[i] = str1[i];
		i++;
	}

	int j = 0;
	while (str2[j] != 0)
	{
		dst[i + j] = str2[j];
		j++;
	}
	dst[i + j] = 0; //字符串结束符

	printf("dst = %s\n", dst);

	return 0;
}

参考:
[1]C基础讲义2018修订版(黑马程序员)


posted @ 2021-07-18 22:01  言非  阅读(293)  评论(0编辑  收藏  举报