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 ...
随机推荐
- app设计需注意的
手机上同步photoshop设计稿: ps play应用 设计: 资源: 1.音乐上传问题 音乐控制在2M以内,推荐使用[格式工厂]进行压缩. 2.视频上传问题 为了保证在线的播放效果,上传的视频大小 ...
- asp.net 与数据库操作
<configuration> <system.web><compilation debug="true" targetFramework=" ...
- HTTP协议 (七) Cookie
HTTP协议 (七) Cookie Cookie是HTTP协议中非常重要的东西, 之前拜读了Fish Li 写的[细说Cookie], 让我学到了很多东西.Fish的这篇文章写得太经典了. 所以我这篇 ...
- mysql入门教程
mysql相信大家都非常熟悉,but读音你们都读对了么?MySQL [maɪ ˌɛskjuːˈɛl] [maɪ ˈsiːkwəl] 念 买S奎儿 或 买吸扣 都可以,还有好多容易读错的名词,详情请见I ...
- JavaScript原型链问题
1. 使用new来创建对象(调用构造函数)时,如果return的是非对象(数字.字符串.布尔类型等)会忽而略返回值;如果return的是对象,则返回该对象. 2. 重写原型会切断原型链: foo = ...
- JS判断终端设备跳转PC端、移动端相应的URL
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...
- Airline Hub
参考:http://blog.csdn.net/mobius_strip/article/details/12731459 #include <stdio.h> #include < ...
- MySql超新手入门
https://www.kancloud.cn/thinkphp/mysql-tutorial/36457
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- Unit05 - 抽象类、接口和内部类(上)
Unit05 - 抽象类.接口和内部类(上) 1.static final常量: 1)必须声明同时初始化 2)通过类名来访问,不能被改变 3)建议:常量名所有字母都大写(多个单词间用_) 4) ...