hdu 1062Text Reverse 反转字符串【两种方法 数组法、堆栈】
来源:互联网 发布:上海ug编程培训班 编辑:程序博客网 时间:2024/05/18 01:31
Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3olleh !dlrowm'I morf .udhI ekil .mca
Sample Output
hello world!I'm from hdu.I like acm.HintRemember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<ctype.h>int main(){char str1[1000],str2[1000]={0};int T,i,t,len,j,k;scanf("%d",&T);getchar(); //因为是用的gets,所以要把回车处理 while(T--){gets(str1);len=strlen(str1); //这里不可以放在while外面,因为并没有读入str1,所以len并没有值 t=0;j=0;for(i=0;i<len;i++){if(str1[i]!=' ') //要用单引号 而不是双引号 str2[j++]=str1[i];else //遇到空格的情况 {if(t>0)printf(" ");for(k=j-1;k>=0;k--){printf("%c",str2[k]);}j=0;t++; } if(i==len-1) //此为反转最后一个字符串,先输出空格,因为若只有两个单词,并不执行第二个else,因为最后并不是空格 {printf(" ");for(k=j-1;k>=0;k--)printf("%c",str2[k]); }}printf("\n");}return 0;}#include<stdio.h>#include<stack>using namespace std;int main(){char ch;int i,T;scanf("%d",&T);getchar();while(T--){stack<char> str;while(true){ch=getchar();if(ch==' '||ch=='\n'||ch==EOF){while(!str.empty()){printf("%c",str.top()); //出栈顶元素 str.pop(); //清空栈顶元素 } if(ch=='\n'||ch==EOF)break; //结束的标志 printf(" ");} elsestr.push(ch);}printf("\n");}return 0;}
0 0
- hdu 1062Text Reverse 反转字符串【两种方法 数组法、堆栈】
- hdu 1062 Text Reverse 字符串反转
- HDU 1062 Text Reverse 字符串反转
- HDU 1062 TEXT Reverse(文本反转)
- hdu 1062 Text Reverse 字符串
- HDU--1062:Text Reverse (字符串)
- hdu——1062Text Reverse(反转函数reverse)
- ACM--字母反转--HDOJ 1062--Text Reverse--字符串
- HDU 1062 Text Reverse 字符串处理
- HDU 1062 Text Reverse(字符串处理)
- HDU 1062 Text Reverse 水题 字符串处理
- hdu oj 1062 Text Reverse(字符串)
- HDU 1062 Text Reverse(字符串翻转)
- HDOJ/HDU 1062 Text Reverse(字符串翻转~)
- hdu 1062 Text Reverse
- hdu 1062 Text Reverse
- HDU 1062 TEXT REVERSE
- Hdu 1062 - Text Reverse
- nginx服务器报403 forbidden错误的解决办法
- linux下的find文件查找命令与grep文件内容查找命令
- 你真的了解word-wrap和word-break的区别吗?
- 基于VHDL的按键扫描程序
- Fragment(一)Android Fragment完全解析,关于碎片你所需知道的一切
- hdu 1062Text Reverse 反转字符串【两种方法 数组法、堆栈】
- 【黑马程序员】-----java基础----IO
- HDU 2014 青年歌手大奖赛_评委会打分
- vmware虚拟机复制后网卡设置
- CentOS6.5配置IP地址的三种方法
- cookie与session比较
- 第三篇:VS2012中,调用opencv库,在程序中定义了变量,编译没有问题,运行时会报错“变量使用没有定义”
- HDU 2013 蟠桃记
- 父 shell,子 shell ,export 与 变量传递