C语言字符串单词倒序

题目要求:输入若干个单词,每个单词之间用空格分割,要求将每个单词中的字母倒序输出。

示例输入:abc def ghijkl

示例输出:cba fed lkjihg

C语言程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*函数:inverse
	作用:将buff+star至buff+end之间的数据倒序存储
	参数1:指针首地址
	参数2:要进行倒序的开始位置与首地址间的距离
	参数3:要进行倒序的结束位置与首地址间的距离*/
void inverse(char *buff, int star, int end)		
{
	char *temporary;				//新建char型指针
	int i;							
	temporary = (char*)malloc(sizeof(char)* (end - star));		//申请一块大小为需要进行倒序的数据段的内存
	memset(temporary, 0, end - star);							//将申请下来的内存清零
	for (i = 0; i < (end - star); i++)				//将源数据倒序放置在新申请的内存下
		temporary[i] = buff[end - i-1];
	memcpy(buff + star, temporary, end - star);		//将倒序后的数据替换到源数据内存下
	free(temporary);		//释放申请内存
}
void main()
{
	char buff[1000];
	char ch;
	int word_long = 0, word_flag = 0;
	int i=0;
	gets(buff);
	do
	{
		if (buff[i++] == ' ')
		{
			inverse(buff, word_flag, word_flag+word_long);
			word_flag += word_long+1;
			word_long = 0;
		}
		else
			word_long++;
	} while (buff[i] != '\0');
	inverse(buff, word_flag, word_flag + word_long);
	puts(buff);
	getchar();
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值