有一个字符数组的内容为:"student a am i", 请你将数组的内容改为"i am a student".

/*
有一个字符数组的内容为:“student a am i”,
请你将数组的内容改为"i am a student".
要求:
不能使用库函数。
只能开辟有限个空间(空间个数和字符串的长度无关)。
student a am i
i ma a tneduts
i am a student
*/


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

//交换函数
void Swap(char* start, char* end){
	char temp;
	--end;
	while (start < end){  //若start指向地的址小于end指向的地址
		temp = *start;  //将start指向的内容赋给temp
		*start = *end;  //将end指向的内容赋给start
		*end = temp;  //将temp指向的内容赋给end
		++start;  //start向后移
		--end;  //end向前移
	}
}

//实现函数
void Function(char str[],int len){
	char* start;  
	char* end;
	start = str;  //start指向字符串str的开头
	end = str + len - 1;  //end指向字符串str的末尾'\0'
	Swap(start, end);  //调用交换函数
	start = end = str;  //start和end都指向字符串str的开头
	while (*end != '\0'){  //若end不指向字符串末尾
		++end;  //end向后移
		if (*end == ' '){  //若end指向为空格
			Swap(start, end);  //调用交换函数
			start = end + 1;  //start指向end的后一项内容
		}
	}
	Swap(start, end);  //最后调用一次交换函数(最后一个字符段后无空格)
	printf("%s\n", str);  //打印输出结果
}

//主函数
int main(){
	char str[] = "student a am i";
	int len = sizeof(str);  //计算保存字符串长度
	Function(str,len);  //调用实现函数
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值