字符串和指针

1、字符串反转函数

void string_reverse(const char *ptr, char *str)
{
	int len = strlen(ptr), i;

	ptr += (len - 1);

	for (i = 0; i < len; i++)
	{
		*str = *ptr;
		str++;
		ptr--;
	}
}

2、找子串

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

int main()
{	
	int len_str, len_ptr, i;
	char *str = (char *)malloc(sizeof(char) * 32);
	char *ptr = (char *)malloc(sizeof(char) * 32);
	
	printf("Please input :\n");
	scanf("%s%s", str, ptr);

	len_str = strlen(str);
	len_ptr = strlen(ptr);

	if (len_str < len_ptr)
	{
		printf("input error!\n");
		return -1;
	}

	for (i = 0; i < len_str - len_ptr + 1; i++)
	{
		if (strncmp(str + i, ptr, len_ptr) == 0)
		{
			printf("%s 是 %s 的子串\n", ptr, str);
			break;
		}

		if (i == len_str - len_ptr)
		{
			printf("%s 不是 %s 的子串\n", ptr, str);
		}
	}
	
	return 0;
}

3、指针的使用

char *p;
fp = (char *)malloc(sizeof(char)*10);
if(NULL == fp)//判断
{
    printf("malloc failure!");
}
strcpy(fp,"hello");
free(fp);//释放空间

4、用字符指针指向一个字符串

char *string[] = {"How","are","you"};//指针数组

printf("%s\n",string[0]);//string[0]是地址,打印字符串

5、解压文件 tar xvzf  tmp.tar.gz     a b c d

      压缩文件 tar cvzf tmp.tar.gz

加字母v后会显示压缩过程等,可以不加

猜你喜欢

转载自blog.csdn.net/weixin_41030848/article/details/81230079