当前位置: 首页 > article >正文

08:字符串

字符串

  • 1、字符串的定义
  • 2、字符串常见的操作

1、字符串的定义

# include <stdio.h>
/*
	字符串的使用
*/
int main(void)
{
	char a[] = {'H','e','l','l','o'};//定义字符串方式一
	char b[] = "Hello";//定义字符串方式二
	char* p = "Hello";//定义字符串方式三
	
	for(int i =0; i < sizeof(a)/sizeof(a[0]); i++)//方式的打印方式
	{
		printf("%c",a[i]);
	}
	puts("");
	for(int i =0; i < sizeof(b)/sizeof(b[0]); i++)//方式二的打印方式
	{
		printf("%c",b[i]);
	}
	puts("");
	for(int i =0; i < 5; i++)//方式二的打印方式
	{
		printf("%c",p[i]);//*(p+i)
	}
	puts("");
	
	printf("%s\n",a);
	printf("%s\n",b);
	printf("%s\n",p);//方式三的打印方式

	puts(a);
	puts(b);
	puts(p);//方式三的打印方式
	
	return 0;
}

如上面的代码,字符串有3种定义方式,但是前面的2中能改变字符串里面的字符,而第三种是定义的字符串常量,定义好了就不能够改变里面的字符了。
代码如下:

# include <stdio.h>
/*
	字符串的使用
*/
int main(void)
{
	char a[] = {'H','e','l','l','o'};//定义字符串方式一
	char b[] = "Hello";//定义字符串方式二
	char* p = "Hello";//定义字符串方式三
	
	a[0] = 'm';
	b[0] = 'm';
	*(p+0) = 'm';//error,这样会使程序崩溃
	
	printf("%c\n",a[0]);
	printf("%c\n",b[0]);
	//printf("%c\n",p[0]);
	
	return 0;
}

我们再来看看定义的字符串占用了几个字节喃?

# include <stdio.h>
# include <string.h>
/*
	字符串占用字节
*/
int main(void)
{
	int m[3] = {1,2,3};
	char n[] = {'n','i','h','a','o'};
	char a[] = {'H','e','l','l','o','\0'};
	char b[] = "Hello";
	char* p = "Hello";
	
	/*
		sizeof()函数:用于计算出变量占内存空间多少个字节。
	*/

	printf("%d\n",sizeof(a));//5给字节
	printf("%d\n",sizeof(b));//6个字节
	printf("%d\n",sizeof(p));//8个字节
	printf("%d\n",sizeof(p[0]));//1个字节
	printf("%d\n",sizeof(m));//12个字节
	
	puts("................");
	
	/*
		strlen()函数:用于计算字符串中有多个有效字符
		以'\0'标识符为结束符。
	*/
	printf("%d\n",strlen(n));//6
	printf("%d\n",strlen(a));//5
	printf("%d\n",strlen(b));//5
	printf("%d\n",strlen(p));//5
	return 0;
}
a变量占用了5个字节。因为里面有5个占用1个字节的元素
b变量占用6个字节,5个字节+'\0'。'\0'为字符串的结束字符
p占用8个字节,因为p为指针变量。
m占用12个字节
n没有'\0'结束符,所以为6
abp都有'\0'结束符,所以都是5

2、字符串常见的操作

①puts的使用:

#include <stdio.h>
#include <string.h>
 
int main()
{
	//第1种操作:puts(),此函数是对字符串打印,并且自动添加换行
	char* p = "woshinibaba";
	puts(p);
	printf("%s\n",p);
	return 0;
}

woshinibaba
woshinibaba

②scanf/gets的使用:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	//第2种操作:scanf()/gets(),此函数是对字符串录入
	char* p  = (char*)malloc(5);//动态构造5个字节,给char类型的变量用
	puts("请输入字符串:");
	//scanf("%s",p);//输入字符串
	gets(p);//输入字符串
	puts(p);
	free(p);//释放空间
	return 0;
}

请输入字符串:
adadadw
adadadw

③字符串的拷贝:strcpy/strncpy

#include <stdio.h>
#include <string.h>
int main()
{
	//第2种操作:实现字符串的拷贝
	char a[128] = {'\0'};
	char b[128] = {'\0'};
	char* p = "wojintianhaoxiangni";
	strcpy(a,p);//将p的字符串拷贝到a
	strncpy(b,p,5);//将p的前5个字符拷贝到a
	puts(a);//打印输出a
	puts(b);//打印输出b
	return 0;
}

wojintianhaoxiangni
wojin

④字符串的拷贝函数的构造:

#include <stdio.h>

void Strcpy1(char* a,char* p);
void Strcpy2(char* a,char* p);
void Strcpy3(char* a,char* p);
void Strncpy(char* b,char* p,int count);

int main()
{
	//第2种操作:实现字符串的拷贝
	char a[128] = {'\0'};
	char b[128] = {'\0'};
	char* p = "wojintianhaoxiangni";
	Strcpy2(a,p);//将p的字符串拷贝到a
	Strncpy(b,p,5);//将p的前5个字符拷贝到a
	puts(a);//打印输出a
	puts(b);//打印输出b
	return 0;
}

void Strcpy1(char* a,char* p)//拷贝函数一
{
	//将p的字符串拷贝到a
	while(*p != '\0')
	{
		*a = *p;//将p的字符串拷贝到a
		p++;//地址的偏移
		a++;
	}
	*a = '\0';//给a的最后加上字符串结束符\0
}

void Strcpy2(char* a,char* p)//拷贝函数二
{
	//将p的字符串拷贝到a
	while(*p != '\0')
	{
		*a++ = *p++;//将p的字符串拷贝到a
	}
	*a = '\0';//给a的最后加上字符串结束符\0
}

void Strcpy3(char* a,char* p)//拷贝函数三
{
	//将p的字符串拷贝到a
	while((*a++ = *p++) != '\0');
	*a = '\0';//给a的最后加上字符串结束符\0
}

void Strncpy(char* b,char* p,int count)//拷贝函数四
{
	//将p的字符串拷贝到a
	while(*p != '\0' && count > 0)
	{
		*b++ = *p++;//将p的字符串拷贝到a
		count--;
	}
	*b = '\0';//给a的最后加上字符串结束符\0
}

wojintianhaoxiangni
wojin

⑤字符串的拼接strcat的使用和构造:

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

void Strcat1(char* a,char*p);

int main()
{
	//第2种操作:实现字符串的拷贝
	char a[128] = "wojintian";
	char* p = " haoxiangni";
	//strcat(a,p);//将p的字符串拼接到a,
	Strcat1(a,p);
	puts(a);//打印输出a
	return 0;
}

void Strcat1(char* a,char*p)
{
	while(*a != '\0')
	{
		a++;//地址偏移
	}
	while(*p != '\0')
	{
		*a = *p;
		  a++;
		  p++;
	}
	*a = '\0';
}

wojintian haoxiangni

⑥字符串的比较strcmp:
如果2给字符串相等则函数返回的是0,不相等返回的1/-1

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

int main()
{
	char* a = "woshinibaba";
	char* p = "woshinibaba";
	int ret = strcmp(a,p);
	if(strcmp(a,p) == 0)
	{
		puts("这2个字符串相等");
	}
	printf("%d\n",ret);
	return 0;
}

这2个字符串相等
0


http://www.kler.cn/news/283195.html

相关文章:

  • 用mintupgrade工具将Linux Mint 21.3升级到Linux Mint 22失败的解决办法
  • Python私教张大鹏FastAPI开源框架和项目第一次整理 20240830
  • chapter09-OOP高级部分——(抽象类模版设计模式)——day12
  • Android APK打包脚本
  • 非阻塞式定时器 apscheduler
  • 力扣8.28
  • 2024年八大在线流程图工具推荐,快来试试吧!
  • 基于asp.net的在线考试系统源码分享
  • Mysql8.x配置详解
  • 回归预测|基于CNN-LSTM-Attention结合Adaboost集成数据预测Matlab程序 多特征输入单输出
  • 喜羊羊做Python二级(模拟考试--易错点)
  • 算法练习: 矩阵置零
  • 对于虚拟机上的相关命令
  • leetcode 19.删除链表的倒数第N个结点
  • LuaJit分析(七)LuaJit -b 命令分析
  • Linux基础 -- 网络工具之curl使用
  • 【JAVA】后端开发中的数据结构:基础知识与应用场景
  • 从 7000 余项目脱颖而出,飞轮科技《新一代实时分析数据仓库解决方案》荣获 HICOOL 2024 全球创业大赛二等奖
  • Oracle字符串聚合函数LISTAGG
  • AI创新,DataOps聚能 | 白鲸开源DTCC共话DataOps新篇章
  • 封装信号灯集相关API
  • 【JavaEE】深入浅出 Spring AOP:概念、实现与原理解析
  • HarmonyOS(AIP12 Beta5版)鸿蒙开发:选择条件渲染和显隐控制
  • 全志/RK安卓屏一体机:智能家居中控屏,支持鸿蒙国产化
  • Android studio设置国内镜像代理(HTTP Proxy)教程详解
  • PTA L1-027 出租
  • leedCode - - - 动态规划
  • 常见的深度学习模型总结
  • Elasticsearch7.15版本后新版本的接入
  • 三级_网络技术_54_应用题