字符串相关算法

/* 字符串逆转 */
void my_reverse(char *p)
{
	int len = strlen(p);
	for (int i = 0;i < len/2; ++i) {
		int tmp = p[i];
		p[i] = p[len-1-i];
		p[len-1-i] = tmp;
	}
}

/* 小写转大写 */
void atoA(char *p)
{
	while (*p) {
		if (*p > 'a' && *p < 'z') {
			*p = *p - 'a' + 'A';
			p++;
		}
		else {
			p++;
			continue;
		}
	}
}

/* 大写转小写 */
void Atoa(char *p)
{
	while (*p) {
		if (*p > 'A' && *p < 'Z') {
			*p = *p - 'A' + 'a';
			p++;
		}
		else {
			p++;
			continue;
		}
	}
}

/* 字符串拼接 */
void my_strcat(char *p1, const char *p2)
{
	char *p = p1;
	while (*p) p++;
	while (*p2) {
		*p = *p2;
		p++;
		p2++;
	}
	*p = '\0';
}

/* 字符串拷贝 */
void my_strcpy(char *p1, const char *p2)
{
	while (p1 != NULL && *p2) {
		*p1++ = *p2++;
	}
	*p1 = '\0';
}

/* 数字转字符串 */
void my_itoa(char *p1, const int number)
{
	char *p = p1;
	int num = number;
	while (num) {
		int n = num%10;
		*p1 = n + '0';
		p1++;
		num /= 10;
	}
	*p1 = '\0';
	my_reverse(p);
}

/* 字符串转数字 */
int my_atoi(const char *p1)
{
	static int n = 0;
	while (*p1) {
		n *= 10;
		n += *p1 - '0';
		p1++;
	}
	return n;
}

/*  字符串排序(小->大) */
char *strSort(char *str)
{
	int len = strlen(str);
	for (int i = 0; i < len-1; ++i) {
		for(int j = 0; j < len-i-1; ++j){
		    if(str[j] > str[j+1]){
				char tmp = str[j];
				str[j] = str[j+1];
				str[j+1] = tmp;
			}
		}
	}
	return str;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗里い着迷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值