C语言8-数组3-一维字符型数组、二维字符型数组

摘要:本文介绍了C语言中字符数组和字符串处理的相关知识。主要内容包括:一维字符数组的常用操作(strcpy、strcat、strcmp)及其注意事项;二维字符数组的定义、初始化与遍历方法;以及通过编程练习实现字符串长度计算(strlen)、字符串拷贝(strcpy)和字符串拼接(strcat)的功能。文中提供了详细代码示例,强调操作时要注意数组边界检查,避免越界访问。这些基础操作是C语言字符串处理的核心内容,对理解更复杂的字符串算法具有重要意义

一、一维字符型数组

1、 字符串拷贝—strcpy

(1)使用范围:将字符串开头到 '\0’部分的内容进行拷贝
(2)使用方法:使用时要用头文件—#include <string.h>
strcpy(dsp,str); 其作用将后边这个源字符串str拷贝到前面目标空间dsp中。

#include <stdio.h>
#include <string.h>
int main(void)
{
	char str[32] = {"hello world"};
	char dsp[32] = {0};
	
	strcpy(dsp,str);
	printf("dsp = %s\n str = %s\n",dsp,str);
	
 	return 0;
}

(2)注意事项:目的空间大小一定大于源字符串空间大小,否则会越界访问导致程序崩溃。

2、字符串拼接—strcat

(1)使用范围:将字符串开头到 '\0’部分的内容进行拷贝
(2)使用方法:使用时要用头文件—#include <string.h>
strcat(dsp,str); 将两字符串拼接成一个字符串
(3)注意事项:要注意被拷贝空间的大小,小心越界访问

3、字符串比较—strcmp

(1)使用方法:使用时要用头文件—#include <string.h>
ret = strcmp(str1,str2); 将两个字符串进行比
①两个字符串相等返回 ret = 0
②第一个字符串大于第二个字符串返回 ret > 0
③第一个字符串小与第二个字符串返回 ret < 0
(2)示例:

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

int main(void)
{
	int ret = 0;
	char str1[32] ={"hello"};
	char str2[32] = {"hello world"};
	
	ret = strcmp(str1,str2);
	printf("%d\n",ret);
	
	return 0;
}

二、二维字符型数组

1、定义:二维字符型数组是一维字符型数组的数组
2、基本形式: char 数组名[行下标][列下标];
3、存储:连续性、有序性
4、数组的初始化(常用的):
(1)全部初始化:char str[2][6] = {"hello’‘,’‘world’‘};
(2)局部初始化:char str[2][6] = {0}; //内其他元素全为零,最常用的初始化
(3)默认初始化:char str[ ][6] = {’‘hello’‘,’‘world’'} //两列俩行
5、打印接收:要利用循环,一行一行的接收。

#include <stdio.h>
int main(void)
{
	int i =0;
	char str[2][5] = {"hel","lop"};

	for (i = 0;i < 2;i++)
	{
		printf("%s",str[i]);
	}
	printf("\n");
	
	return 0;
}

三、练习

1、编写程序,不使用strlen完成strlen的功能。
(1)while循环实现

#include <stdio.h>
int main(void)
{
	int i = 0;
	char str[32] = {"hello world!"};
	
	while (str[i] != '\0')
	{
		i++;	
	}
	printf("len:%d\n",i);
	
	return 0;
}

(2)for 循环实现

#include <stdio.h>
int main(void)
{
	int i = 0;
	char str[32] = {"hello world!"};

	for (i = 0;str[i] != '\0';i++)
	{
	}
	printf("len=%d\n",i);
	return 0;
}

2、编写程序,不使用strcpy完成strcpy的功能。
(1)while循环实现

#include <stdio.h>

int main(void)
{
	int i = 0;
	char str[32] = {"hello world"};
	char dst[32] = {0};
	
	while(str[i] != '\0')
	{
		dst[i] = str[i];
		i++;
	}
	dst[i] = '\0';
	printf("dst=%s",dst);
	return 0;
}

(2)for循环实现

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

int main(void)
{
	int i = 0;
	int len = 0;
	char str[32] = {"hello world"};
	char dst[32] = {0};

	len = strlen(str);

	for (i = 0; i <= len;i++)
	{
		if (i < len)
		{
			dst[i] = str[i];
		}
		else
		{
			dst[i] = str[i];
		}
	}
	printf("dst = %s\n",dst);
}
	return 0;
	

3、编写程序,不使用strcat完成strcat的功能。
若某字符串的len的长度假设是4(1、2、3、4);
那该字符串的’\0’也在位置4处(0、1、2、3、【4为’\o’】);

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

int main(void)
{
	int i = 0;
	int j = 0;
	int len1 = 0;
	int len2 = 0;
	char str[32] = {"hello"};
	char dst[32] = {" world"};

	len1 = strlen(str);
	len2 = strlen(dst);

	j = 0;
	for (i = len1;i < (len1+len2);i++)
	{
		str[i] = dst[j];
		j++;
	}
	printf("str+dst= %s\n",str);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值