hdu_1062 Text Reverse(水题)

本文介绍了一种简单的字符串处理方法,即如何将输入句子中的每个单词进行反转,并保持原有空格不变。通过C++实现,适用于初学者理解字符串操作的基本概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:click here~~

题意是给出一个句子,把句子中的每个单词反转,空格原样输出。

#include <cstdio>
#include <cstdlib> 
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int n;
	char a[1005];
	char temp[1005];
			
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		memset(a,'\0',sizeof(a));
		memset(temp,'\0',sizeof(temp));
		gets(a);
		a[strlen(a)] = ' ';
		int start = 0;
		for(int i = 0; i < strlen(a); i++)
		{
			if(a[i] != ' ' && a[i] != '\t')
				temp[start ++] = a[i];
			else
			{
				//if(start == 0) continue;
				//else{
				for(int j = start - 1; j >= 0; j--)
					printf("%c",temp[j]);
				//printf("%c",temp[0]);
				if(i == strlen(a) - 1)
					printf("\n");
				else printf("%c",a[i]);
				memset(temp,'\0',sizeof(temp));
				start = 0;
				//}
			}
		}
	}
	//printf("\n");
}

如果有更好更简洁代码,欢迎评论交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值