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 ...
随机推荐
- linux安装memcached及memcache扩展
一.安装libevent函数库 下载地址:http://libevent.org 默认被安装到:/usr/local/lib目录下 安装memcached之前需要先安装libevent函数库. 可以通 ...
- Orale介绍
Oracle数据库: 是甲骨文公司的一款关系数据库管理系统 具有网格计算的框架 数据量大,并发操作比较多,实时性要求高,采取ORACLE数据库 Oracle数据库的体系结构包括物理存储结构和逻辑存储结 ...
- 响应式网站通用css
/* core.css v1.1 | MIT License | corecss.io */ html { font-family: sans-serif; font-size: 100%; line ...
- 使用本地phpmyadmin管理远程数据库
使用放下挺简单的,因为某些业务需求,phpmyadmin不让放在网站服务器上,所以放在本地可以方便管理远程数据库 点击下载会下载一个 config.inc.php 吧这个文件放在phpmyadmin ...
- jdk动态代理学习
在jdk的好多底层代码中很多都使用jdk的动态代理,下面就写写简单的代码来look look. 老规矩先上代码: public interface SayDao { public String say ...
- Mysql VARCHAR(X) vs TEXT
一般情况下,我们不太会纠结用Varchar或text数据类型. 比如说,我们要存储邮箱,我们自然会用varchar,不会想到用text.而当我们要存储一段话的时候,选了text,感觉varchar也够 ...
- Postgre cannot insert multiple commands into a prepared statement
悲剧... FireDAC连接Postgre数据库, 使用默认的属性, 一次执行多条SQL的时候, 会报"cannot insert multiple commands into a pre ...
- 关于webapi post 使用多个参数的间接用法
问题描述: Web Api 当使用Post提交的时候 由于只能接受一个参数 ,所以我们基本都会选择把所需要的参数,进行封装实体. 有的时候所需要的信息在其他两个实体中,还需要重新封装也不爽. 今天发现 ...
- 使用apache.lang包安全简洁地操作Java时间
引言 最近偶遇apache开发的工作java工具包,一使用,就发现自己爱上它了.不多说了,下面介绍org.apache.commons.lang3.time包中处理java程序员为之头疼的时间的类. ...
- C#中的泛型 【转】
C#中的泛型 泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确 ...