1323. Switch text
Description
The program must switch the text lines in a backward order and split them by the middle, processing the input lines in pairs. If an empty or blank line is found, it is considered as a line but it is not printed out to the output.
Input
The input will be a text file with text lines.
Output
Standard output with the switched and splitted lines.
Sample Input
This lines must be printed backwards and splitted in the middle.
And each line too!
hellow my friend
how are you today
i hope you're fine
be cool, be nice.
Sample Output
hcae dnA!oot enil
wkcab detnirp eb tsum senil sihT.elddim eht ni dettilps dna sdra
y era wohyadot uo
m wollehdneirf y
oy epoh ienif er'u
,looc eb.ecin eb
注意空格回车的判断。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool checkEmptyLine(string str)
{
for (int i = 0; i < str.size(); i++)
{
if (str[i] != ' ')
return true;
}
return false;
}
void calculate(string str)
{
string result,final,p1,p2;
int len=str.length();
if(checkEmptyLine(str))
{
result=str;
for(int i=0;i<len;i++)
result[i]=str[len-1-i];
p1=result.substr(0,len/2);
if(len%2==0)
p2=result.substr(len/2,len/2);
else p2=result.substr(len/2,len/2+1);
final=p2+p1;
cout<<final<<endl;
}
}
int main()
{
string str,str2;
while(getline(cin,str))
{
getline(cin,str2);
calculate(str2);
calculate(str);
}
return 0;
}