C语言/C++常见字符串函数

#include<iostream>
#include<conio.h>//_getch()函数头文件
#include<cstring>
#pragma warning(disable : 4996)  
#define _CRT_SECURE_NO_WARNINGS
using namespace std;

void OutChar()//输出字符
{
	
	char c =0;
	char d;
	cin >> c >> d;
	cout << c << d << endl;

	char e = getchar();
	cout << e << endl;

	char f;
	f=_getch();//输入不显示,输入结束不用按回车,立即响应,贪吃蛇方向键
	cout << f << endl;

}
void InString()//输入字符
{
	//注意重复对一个数组输出的情况

	/*
	char co1[10];
	cout << "cin输入:" << endl;
	cin >> co1;//输入字符中存在空格就截止了
	cout << "cout输出:" << endl;
	cout << co1 <<endl;
	*/

	//由于cin cout和gets_s puts冲突,后者会使用前者输出的剩余数据,先将前者注释!!


	char sss1[10];
	cout << "gets_s输入" << endl;
	gets_s(sss1,5);//识别空格,给\0留一个空位,当前数组最多输入4字节否则报错!!
	cout << "puts输出" << endl;
	puts(sss1);
	cout << endl;

	cout << "puts直接输出汉字" << endl;
	char sss2[3] = "周";//一个汉字占2字符!!!,字符数组结尾留一个空位给\0
	puts(sss2);
	cout << endl;
	
	char str_array[5] = { '\0' };
	cout << "fgets输入" << endl;
	fgets(str_array, sizeof(str_array), stdin);
	cout << "fputs输出" << endl;
	fputs(str_array, stdout);
	//从标准输入中读入字节数 - 1个字节的字符,就是说当前数组输入5字节只能读4字节,但不报错!!
}
void DXXchange()//字符大小写转换
{

	cout << "字符大小写转换" << endl;
	while (1)
	{
		char h = 0;
		cout << "请输入所要转换的字符" << endl;
		cin >> h;
		if (h >= 65 && h <= 90)//h为小写字母'a'~'z'
		{
			h += 32;
			cout << h << endl;
			//如果直接输出h+32将会得到输入字符的ASCII码数值!
		}
		else if (h >= 97 && h <= 122)//h为大写字母'A'~'Z'
		{
			h -= 32;
			cout << h << endl;
		}
		else
		{
			cout << "输入有误请重新输入" << endl;
			continue;
		}
		break;

	}

}

void OutString()//输出字符数组
{

	cout << "字符数组:" << endl;
	char arr[4] = { 'a','b','Q','e' };//未初始化默认为空格
	char *p = arr;//&arr[0]
	cout << "循环输出" << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << *p++;//*和++优先级一样但是结合性从右向左,所以先运算++
		//cout << *(p + i);
		//cout << p[i];
		//cout << *(arr + i);
		//cout << arr[i];
	}
	cout << endl;
	cout << "没有'\\0'结尾的字符数组名报错输出" << endl;
	cout << arr << endl;
	//字符数组必须以循环输出,数组名输出成功的原因是数组里有\0字符,其实也就是字符串

	cout << "字符串输出(必须以'\\0'结尾)" << endl;
	char str[3] = { 'c','g','\0' };//不写\0默认也是'\0'
	cout << str << endl;

	char str1[5] = { 'q','f','w','\0','t' };
	cout << "字符串中间有\\0的输出结果:" << str1 << endl;
	//遇到'\0'打印结束
	//字符数组中有\0就算字符串?

	printf("printf从指定位置打印结果");
	printf("%s\n", &str1[1]);

	cout << "puts专属打印字符串函数结果:";
	puts(&str1[0]);//puts本身就是打印不需要借助cout


	cout << "指针循环利用putchar输出";
	for (char*ps1 = str1; *ps1 != '\0'; ps1++)
	{

		putchar(*ps1);
	}
}

void ChangeString()//修改字符串
{
	//常量字符串,可以改变指针指向不可被修改
	const char *c = "world";
	//错误示范c[1] = 'c';

	//可以修改字符串
	char str11[8] = "helloc3";//维度为8串长为7
	//char str11[] = "hello";不写元素个数也可以
	//字符串维度=串长+1
	//维度可以用sizeof求得,即包含\0,串长不包含
	cout <<"原str11字符串为"<< str11<<endl;


	str11[2] = 'u'; //修改字符
	cout << "修改单个字符后为" << str11 << endl;



	//对字符串整个重新赋值,注意重新赋值时,替换的字符串不要比被替换的大,否则越界
	int i = 0;
	for (const char *c1 = "change"; *c1 != '\0'; c1++)//将change字符串全部赋值给str11知直到\0结束
	{
		str11[i] = *c1;
		i++;

	}
	//"change"串长为6,赋值一次i+1;最终赋值完成后i=6,
	//"helloc3"串长为7所以还剩下一个'3'没有修改

	cout << "不加\\0修改结果为" << str11 << endl;

	//i在赋值后多加一次,在change的后一个位置正好用\0赋值截断
	str11[i] = '\0';//只有加上这个截断,才不会保留原来字符串剩余的内容
	cout << "加\\0指针循环修改整个字符串结果为" << str11 << endl;


}


//字符串常用函数
void String_Fun()
{
	const char *p = "12345";

	size_t a =strlen(p);//由无符号的整形a传递结果
	//参数还可以为strlen("12345");或者字符数组名strlen(str);
	cout<<"调用strlen求串长为"<< a << endl;

	int b = strcmp("abcd", "abef");
	//挨个比较到不一样结束,后面的不看,但是"abc"和"abcd"比较后者大
	//第一个大返回1,第二个大返回-1,一样大返回0,所以用int b来接
	cout << "strcmp比较结果为" << b << endl;

	int c = strncmp("ahcd", "abef",2);//2代表只比较前2个字符

	cout << "strncmp比较前2个字符结果为" << c << endl;
	//strncmp返回结果>0 <0 =0 三种,不是固定的+-1


	char str[10] = "zxcv";
	strcat(str, "asd");
	//strcat_s(str,10,"sed");最大字节10保证不越界
	cout <<"strcat末尾拼接结果为"<< str << endl;

	strncat(str, "qqww",2);
	cout << "strncat末尾拼接前2元素结果为" << str << endl;

	char str11[8] = "helloc3";
	strcpy(str11, "coppy");//第一个位置必须为数组名,第二个可以为指针,字符串,数组名
	cout << "调用strcpy函数替换整个字符串结果为" << str11 << endl;

	strcpy_s(str11, 8, "coppys");//中间参数为str11最大复制字节数,防止越界
	cout << "调用strcpy_s函数替换整个字符串结果为" << str11 << endl;

	strncpy(str11, "qwertyyrqw", 3);//用后面的修改coppys前面的部分
	cout << "调用strncpy函数修改前3个字符串结果为" << str11 << endl;

	strncpy_s(str11, 8, "cvbrdtysa", 4);//用第三个参数的前4个字符覆盖整个前面的字符串
	cout << "调用strncpy_s函数替换整字符串结果为" << str11 << endl;

	//不带_s在程序结束之后报错
	//带_s后缀的函数报错较为及时,在指定那一行函数直接报错,且多一个参数限制防止越界,

	char str_array4[10] = "world";
	printf("============strchr=================\n");//判断字符串中是否包含字符,返回的是这个字符的指针(从左往右边)
	char *p4 = strchr(str_array4, 'o');//若不包含报错
	printf("p4:%s,%c\n", p4, *p4);

	char *ptr_string_5 = strrchr(str_array4, 'o');//函数名多一个r,从右往左寻找,如果不包含报错
	printf("ptr_string_5:%s,%c\n", ptr_string_5, *ptr_string_5);

	if (strchr(ptr_string_5, 'o') != NULL) 
	{
		printf("ptr_string_5 contains包含 o!\n");
	}
	else 
	{
		printf("ptr_string_5 not contains不包含 o!\n");
	}

	printf("==================strstr===========\n");//字符串中是否包含另外一个字符串
	const char *str9 = "hello";
	const char *str10 = "first helloworld";
	if (strstr(str10, str9) != NULL) {
		cout<<"str10 包含 str9\n";
	}
	else
	{
		cout << "str10 不包含 str9" << endl;
	}

	cout<<"=================strtok字符串分割函数=============\n";

	char strfg[] = "hello:zzf:hello:tom";
	cout << "分割前" << strfg << endl;
	int counter = 0;
	char *pfg = strtok(strfg, ":");
	cout << "分割后:";
	while (pfg != NULL)
	{
		counter++;
		printf("%s", pfg);
		pfg = strtok(NULL, ":");
	}

	cout << endl;
	printf("================memset,memcpy==========");//内存清空和拷贝
	char src[] = "i love you";
	char des[] = "you love me";
	memset(des, 0, sizeof(des));//清空或者初始化
	memcpy(des, src, sizeof(des) - 1);//拷贝des字节数-1,最后一个符号用于存储结束符号
	cout << endl;


	int d = atoi("123wer");
	cout << "atoi将字符串中的连续整数取出打印:" << d << endl;
	//必须数字开头,否则打印为0,"123we5r" 后面不连续的数字5也不打印

	char str2[10] = { 0 };
	itoa(123, str2, 16);//将123以16进制形式转换后放在str2数组中,,8进制2进制也可以按需要修改参数
	cout << "将整数转换为16进制为:" << str2 << endl;
	//_itoa_s多参数,防止越界
}
void ZYString()
{
	cout << "\\0 \' \" \n ";
	//,"\0等与系统重复的字符,cout打印前都要在前面多加上转义字符 \ 否则会冲突
	//E:\\桌面\\代码 需要双\\ 标识
}
void StringArrary()//字符串数组与二维字符数组
{

	
	const char* str[3] = { "abc","qwe","123" };//字符串数组操作堆区申请空间

	cout << str[0]<<endl;
	//*str[0] = "f";错误,str[0]是字符a的首地址,取*不可修改常量字符串中的a,只可读常量字符串

	str[0] = "zxc";//正确!重新指向新的常量字符串
	cout << str[0] << endl;
	

	//操作栈区复制的二维字符数组空间
	char str1[2][4] = { "vbn","sda" };//可以装2个字符串,每个字符串串长为3,留一个给\0
	cout << str1[0][1]<<endl;
	cout << str[1];

}
int main()
{

	//OutChar();
	//InString();
	//DXXchange();
	//OutString();
	//ChangeString();
	String_Fun();
	//ZYString();
	//StringArrary();
	system("pause");
	return 0;
}
发布了20 篇原创文章 · 获赞 26 · 访问量 3561

猜你喜欢

转载自blog.csdn.net/qq_42837890/article/details/104329290
今日推荐