1. 程式人生 > 其它 >Text Reverse hdu-1062 C++

Text Reverse hdu-1062 C++

技術標籤:HDU演算法

題目連結

在這裡插入圖片描述

解析:
字串反轉。。
輸入多個用例
每個用例都當做是字串
遍歷字串,遇到空格即將當前位置的前一個字元到上個空格(如果有的話)之間的子字串翻轉輸出,再輸出空格,
遇到結束符‘\0’即將當前位置的前一個字元到上個空格(如果有的話)之間的子字串翻轉輸出,再輸出換行

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;
    string s;
    cin >> n;
    getchar();
    while
(n--) { getline(cin, s); int k = s.length(); int tempz =-1,tempy=-1; for (int i = 0; i <= k; i++) { if (s[i] == ' ') { tempz = tempy+1; tempy = i; for (int j = tempy-1; j >= tempz;
j--) { cout << s[j]; } cout << " "; } if (s[i]=='\0') { tempz = tempy+1; tempy = i; for (int j = tempy-1; j >= tempz; j--) {
cout << s[j]; } cout << endl; } } } return 0; }