Word Reversal
For each list of words, output a line with each word reversed without changing the order of the words.
Input
You will be given a number of test cases. The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.
Output
For each test case, print the output on one line.
Sample Input
3
I am happy today
To be or not to be
I want to win the practice contest
Sample Output
I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc
#include <iostream>
#include <string>
#include <algorithm>
#include<cstdio>
using namespace std; int main()
{
int cas;
cin >> cas;
getchar();//此处处理回车字符 记住 如果不处理 会出现问题的
while (cas--)
{
string s, t;
getline(cin,s);
t.clear();//每个测试案例之前都要清理掉t 它就是一个类似字符串(就是一个单词)的操作空间
int flag=;
//char str= ' '; 一个空字符不能用字符数组来表示
for ( int i = ; i < s.length() ; i++)
{
//看不太懂你核心代码的意思 有时间可以探讨一下
/* int pos = s.find(str,i);//这儿实现比较复杂
cout<<pos;
int j = s.length() - pos;
int a;
for(; a <= pos && j <= s.length(); a++ , j++)
{
s[a] = t[j];
}
a += pos + 1;*/
//代码思想是:1、不是空格且没到字符串的长度 就把s中的一个一个字母放到t中
// 2、遇到空格 就把它翻转 reverse函数 ;空格 flag来设置,
if(s[i]!=' '&&i<=s.length()-)
t+=s[i];
else
{
reverse(t.begin(),t.end());
if(flag) cout<<" ";
cout<<t;
flag=;//遇到空格 就把flag设置为1 ,等下一个单词t 输出之前 把空格输出来
t.clear();//t 也就是一个单词 故处理下一个单词时 把他清理掉 } }
//这部分你的想法是对的
reverse(t.begin(),t.end());//最后一个单词 它不会出现空格 故放在for循环外面处理
cout<<" "<< t << endl; //输出之前要加空格
} return ;
}
Word Reversal的更多相关文章
- zoj1151 zoj1295 Word Reversal 字符串的简单处理
Word Reversal Time Limit: 2 Seconds Memory Limit:65536 KB For each list of words, output a line ...
- ZOJ 1151 Word Reversal
原题链接 题目大意:给一句话,把每个单词倒序,然后输出. 解法:我是用了一个堆栈,以空格来拆分单词,把每个字母压入堆栈,然后依次输出. 参考代码: /* * 字符串反向,140ms,188kb * 单 ...
- ZOJ 1151 Word Reversal反转单词 (string字符串处理)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 For each list of words, output a l ...
- zoj 1151 Word Reversal(字符串操作模拟)
题目连接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1151 题目描述: For each list of words ...
- Word Reversal (简单字符串处理)
题目描述: For each list of words, output a line with each word reversed without changing the order of th ...
- Word Reversal(string)
For each list of words, output a line with each word reversed without changing the order of the word ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
随机推荐
- Java中的Bigdecimal类型运算
Java中的Bigdecimal类型运算 双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更小的数进行运算和处理.Java在java.math包中提 供的API类BigD ...
- SQL Server 2008 下载及安装教程
sql server 2008 是微软公司开发的一套数据库管理系统.是目前大型数据库中常用数据库之一.性能稳定,功能强大,是面向中大型企业的一款数据库解决方案.我们安装SqlServer2008的时候 ...
- js 查找关键字
查找:4种: 1. 查找固定关键字,仅返回位置,可指定开始位置: var i=str.indexOf("kword"[,starti]); str.lastIndexOf(&quo ...
- jQuery上定义插件并重设插件构造函数
插件alert的全部代码,每个插件都定义在如下类似的作用域中: +function ($) { 'use strict'; // ALERT CLASS DEFINITION // ========= ...
- BizTalk动手实验(十四)AS2传输非EDI消息
1 课程简介 本课程通过AS2协议完成XML文件的传输过程,传输之后同步接收MDN确认信息.通过本课程熟悉AS2传输解决方案的基本开发与配置方法. 2 准备工作 为了模拟实际的B2B交互过程,本实验需 ...
- 使用Apache的.htaccess就可以防盗链
Apache的.htaccess可以实现很多功能,如密码保护.禁止显示目录列表.阻止/允许特定的IP地址.实现网址的301 重定向等等.本文就来说说使用Apache的.htaccess如何防盗链. 当 ...
- mvc 扩展htmlhelper
using System.Web.Mvc; namespace System.Web.Mvc{ public static class HtmlExtend { public ...
- iOS CAShapeLayer记录
基本知识 看看官方说明: /* The shape layer draws a cubic Bezier spline in its coordinate space. * * The spline ...
- 使用Entity Framework 自动产生的Sql语句
对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...
- C# DataGrid合并单元格
1.栏位枚举 private enum DataGridColumn { ROWNUM = , EMPID, EMPNAME, SEX, SALARY, ADRRESS, PHONE, TEL, PO ...