Loading

指针操作字符串


返回 我的技术栈(Technology Stack)



两个字符串相加

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

void my_strcat1(char ch1[], char ch2[])
{
	int i = 0;
	while (ch1[i] != '\0')
	{
		i++;
	}
	printf("%d\n", i);   //输出:5
	printf("%d\n", strlen(ch1));  //输出:5

	int j = 0;
	while (ch2[j] != '\0')
	{
		ch1[i + j] = ch2[j];
		j++;
	}
	//ch1[i + j] = 0;      可以不写,因为 ch1 指定的是100,不够的话,默认是自动添加 0 的,而0又相当于'\0'
}

void my_strcat2(char* ch1,char* ch2)
{
	int i = 0;
	while (ch1[i] != '\0') 
	{
		i++;
	}
	printf("%d\n", i);   //输出:5
	printf("%d\n", strlen(ch1));  //输出:5

	int j = 0;
	while (ch2[j] != '\0') 
	{
		ch1[i + j] = ch2[j];
		j++;
	}
	//ch1[i + j] = 0;     可以不写,因为 ch1 指定的是100,不够的话,默认是自动添加 0 的,而0又相当于'\0'
}


void my_strcat3(char* ch1, char* ch2)
{
	int i = 0;
	while (*(ch1+i) != '\0')
	{
		i++;
	}
	printf("%d\n", i);   //输出:5
	printf("%d\n", strlen(ch1));  //输出:5

	int j = 0;
	while (*(ch2+j) != '\0')
	{
		*(ch1 + i+j) = *(ch2 + j);
		j++;
	}
	//ch1[i + j] = 0;     可以不写,因为 ch1 指定的是100,不够的话,默认是自动添加 0 的,而0又相当于'\0'
}

void my_strcat4(char* ch1, char* ch2)
{
	while (*ch1)
	{
		ch1++;  // 这个是指针里存储的地址  增加偏移量
	}

	while (*ch2)
	{
		*ch1 = *ch2;
		ch1++;  
		ch2++;
	}
}

void my_strcat(char* ch1, char* ch2)
{
	while (*ch1)ch1++;
	while (*ch1++=*ch2++);
}

int main(void)
{
	char ch1[100] = "hello";  // 实际是100,因为初始化指定100
	char ch2[] = "world";   // 实际是6,因为还有一个'\0'结尾

	my_strcat(ch1, ch2);
	//这里函数里面并没有添加 '\0' 的操作,为什么打印不会乱码出错呢?
	//因为 ch1 指定的是100,不够的话,默认是自动添加 0 的,而0又相当于'\0'

	printf("%s\n",ch1);  //输出:helloworld        

	return 0;
}

字符串去除空格

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

void remove_space(char* ch)
{
	//用来遍历字符串
	char* ftemp = ch;
	//记录非空格字符串
	char* rtemp = ch;
	while (*ftemp) 
	{
		if (*ftemp != ' ') 
		{
			*rtemp = *ftemp;
			rtemp++;
		}
		ftemp++;
	}
	*rtemp = 0;
}

int main(void)
{
	char ch[] = "h  el   lo    w   or    ld ";

	printf("%s\n", ch);  //输出:h  el   lo    w   or    ld 

	remove_space(ch);

	printf("%s\n",ch);  //输出:helloworld        

	return 0;
}

寻找字符串中某个字符地址

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

char* my_strindex1(char* str, char ch)
{
	int i = 0;
	while (str[i])
	{
		if (str[i] == ch)
		{
			return &str[i];
		}
		i++;
	}

	return NULL;
}

char* my_strindex(char* str, char ch)
{
	while (*str)
	{
		if (*str == ch)
		{
			return str;
			//return &(*str);
		}
		str++;
	}

	return NULL;
}

int main(void)
{
	char str[] = "hello world";  // 实际是100,因为初始化指定100
	char ch = 'l';

	char* ch_index = my_strindex(str, ch);

	if (ch_index == NULL) 
	{
		printf("找不到!");
	}
	else
	{
		printf("%p\n",ch_index);
	}

	return 0;
}

字符串匹配

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

char* my_strstr(char* src, char* dest) 
{
	char* fsrc = src; //遍历源字符串的指针
	char* rsrc = src; //记录相同字符串在 源字符串的首地址
	char* tdest = dest;

	while (*fsrc) 
	{
		rsrc = fsrc;
		while (*fsrc == *tdest &&*fsrc!='\0')
		{
			fsrc++;
			tdest++;
		}

		if (*tdest == '\0')
		{
			return rsrc;
		}

		//回滚
		tdest = dest;//回到目标字符串的起始位置
		fsrc = rsrc;
		fsrc++;
	}

}

int main(void)
{
	char src[] = "hello world";
	char dest[] = "llo";

	char* p = my_strstr(src, dest);
	printf("%s\n",p);

	return 0;
}

posted @ 2021-07-23 10:48  言非  阅读(174)  评论(0编辑  收藏  举报