Text Reverse——个人c++解

xiaoxiao2021-02-28  101

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.  

Output

For each test case, you should output the text which is processed.  

Sample Input

3 olleh !dlrow m'I morf .udh I ekil .mca

Sample Output

hello world! I'm from hdu. I like acm. [hint] Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it. [/hint] #include <bits/stdc++.h> using namespace std; int main() { int n,i,j,l,k; char a[1001],b; cin>>n; getchar(); while(n--) { gets(a); l=strlen(a); for(i=0;i<=l;i++) { if(a[i]==' ')//每次遇到空格则逆序输出到前一个空格或开始字符 { for(j=i-1;;j--) { if(j==-1)break; if(a[j]==' ')break; printf("%c",a[j]); } printf(" "); } if(a[i]=='\0')//每次到结束'\0'则逆序输出到前一个空格或开始字符 { for(j=i-1;;j--) { if(j==-1)break; if(a[j]==' ')break; printf("%c",a[j]); } printf("\n"); } } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-57938.html

最新回复(0)