c语言面试之字符串

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "assert.h"


// 1 字符串的链接
char *strcat(char *strDes, const char *strSrc)
{
	char *address = strDes;
	assert((strDes != NULL)&&(strSrc != NULL));
	while (*strDes != '\0')
	{
		++strDes;
	}
	while ((*strDes++ = *strSrc++) != '\0')
	{
		NULL;
	}
	return address;
}

void main01()
{
	char str1[] = "12345";
	char str2[] = "abcde";

	printf("str:%s\n", strcat(str1, str2));
	system("pause");
}

// 2 链接n个字符串
char *strncat1(char *strDes, const char *strSrc, int count)
{
	char *address = strDes;
	assert((strDes != NULL) && (strSrc != NULL));
	while (*strDes != '\0')
	{
		++strDes;
	}

	while (count-- && *strSrc != '\0')
	{
		*strDes++ = *strSrc++;
	}
	*strDes = '\0';
	return address;
}

void main02()
{
	char str1[] = "12345";
	char str2[] = "abcde";
	printf("str:%s \n", strncat1(str1, str2, 2));
	system("pause");
}

// 3 字符串比较
int strcmp(const char *s,const char *t)
{
	assert((s != NULL) && (t != NULL));
	while (*s && *t && *s == *t)
	{
		++s;
		++t;
	}
	return (*s - *t);
}

void main03()
{
	char str1[] = "12345";
	char str2[] = "abcde";
	int ret = strcmp(str1, str1);
	printf("%d \n", ret);
	system("pause");
}

// 4 字符串n比较
int strncmp(const char *s, const char *t, int count)
{
	assert((s != NULL) && (t != NULL));
	while (*s && *t && *s == *t && count--)
	{
		++s;
		++t;
	}
	return (*s - *t);
}

void main04()
{
	char str1[] = "12345";
	char str2[] = "abcde";
	int ret = strncmp(str1, str1, 3);
	printf("结果ret = %d \n", ret);
	system("pause");
}

// 5 字符串拷贝
char *strcpy1(char *strDes, const char *strSrc)
{
	char *address = strDes;
	assert((strDes != NULL) && (strSrc != NULL));
	while ((*strDes++ = *strSrc++) != '\0')
	{
		NULL;
	}
	return address;
}

void main05()
{
	char str1[] = "12345";
	char str2[] = "abcde";
	char *str = strcpy1(str1, str2);
	printf("str:%s\n", str);
	system("pause");
}

// 6 拷贝n个字符串
char *strncpy(char *strDes, char *strSrc, int count)
{
	char *address = strDes;
	assert((strDes != NULL) && (strSrc != NULL));
	while(count-- && (*strDes++ = *strSrc++) != '\0')
	{
		NULL;
	}
	return address;
}

void main06()
{
	char str1[] = "12345";
	char str2[] = "abcde";
	char *str = strncpy(str1, str2, 3);
	printf("str:%s\n", str);
	system("pause");
}

// 7 求取字符串的长度
int strlen1(const char *str)
{
	int len = 0;
	assert(str != NULL);
	while (*str++ != '\0')
	{
		++len;
	}
	return len;
}

void main07()
{
	char str1[] = "12345";
	int ret = strlen1(str1);
	printf("ret:%d\n", ret);


	system("pause");
}


// 8 二分查找
int BSearch(elemtype *a, elemtype x, int low, int high)
{
	// 在下届为low,上届为high的数组a中这般查找元素x
	int mid;
	if (low > high)
	{
		return -1;
	}

	mid = (low + high) / 2;
	if (x = a[mid])
	{
		return mid;
	}
	if (x < a[mid])
	{
		return(BSearch(a,x, low, mid - 1));
	}
	else
	{
		return (BSearch(a, x, mid + 1, high));
	}
	return;
}

// 9 字符串逆序
char *Reserve(const char* str)
{
	int i = 0;
	char tmp;

	char *address = str;
	assert(str != NULL);

	for (i = 0; i< strlen(str)/2; i++)
	{
		tmp = str[i];
		str[i] = str[len - i - 1];
		str[len - i - 1] = tmp;
	}

	return address;
}


void main()
{
	char *str = "hello the world";
	int len = strlen(str);

	char *dest = (char *)malloc(len + 1);
	char *address = dest;

	char *end = &str[len - 1];

	while (len-- != 0)
	{
		*address++ = *end++;
	}
	*address = 0;
	system("pause");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值