HackerRank

String Reverse Words

Given n strings, output each string in reverse order. The words in each string should be kept intact.

Friends!! Read the question deeply. It says you can have at max 1000 lines and each line can have 1mn char.

Input Format

First line consists of integer N, number of strings you will be given.

Next N lines contains one string per line.

Constraints

1 ≤ N ≤ 1000
1 ≤ len(string) ≤ 10^6

Output Format

N lines of desired output.

Sample Input

1

At coding blocks, you get well structured syllabus that helps to build your logic and improve programming quotient.

Sample Output

quotient. programming improve and logic your build to helps that syllabus structured well get you blocks, coding At

Explanation

Reversed output with words intact.
[1]:
#include <vector>
#include <iostream>
#include <algorithm>

int main() {

    int n;
    std::cin >> n;

    for (int i=0 ; i<n ; i++)
    {
        std::vector<std::string> line;
        char endc;
        do{
            std::string s;
            std::cin >> s;
            line.push_back(s);
            endc = s.back();
        } while (endc != '.');

        std::reverse(line.begin(), line.end());

        for (const std::string& word : line)
            std::cout << word << " ";

        std::cout << std::endl;
    }

    return 0;
}

main();
 1
 This is a test.
test. a is This
[1]:
# Python

n = int(input())
strings = []

for i in range(n):
    strings.append(input())

for string in strings:
    print(' '.join(string.split(' ')[::-1]))
 1
 This is a test.
test. a is This

Reverse String 4

Implement a function for reversing the words in a string s. Your function should use O(1) space.

NOTE 1 : No checks are provided for memory limit in the environment itself. Memory limits will be checked when a moderator will review the code.

NOTE 2 : It will be helpful if your code has proper indentation and naming of variables.

Input Format

The first line contains a single integer denoting the number of strings to be reversed (say n).

Each line of the subsequent n lines contain a string S to be reversed.

Constraints

0 <= n <=100 0 < length(s) <= 10000

Output Format

Return the reversed strings in new lines.

Sample Input 0

2
123
abcd

Sample Output 0

321
dcba
[2]:
#include <iostream>
#include <algorithm>
#include <string>

int main() {
    int n;
    std::cin >> n;

    for (int i=0 ; i<n ; i++)
    {
        std::string s;
        std::cin >> s;
        std::reverse(s.begin(), s.end());
        std::cout << s << std::endl;
    }
    return 0;
}

main();
 2
 123
321
 abcd
dcba

String Reverse

Implement a program to reverse the sentence of the given statement word by word and then reverse.

Example Input:

Welcome to MCA Department

Output:

tnemtrapeD ACM ot emocleW
[3]:
#include <iostream>
#include <algorithm>

int main() {
    std::string s;
    std::getline(std::cin, s);
    std::reverse(s.begin(), s.end());
    std::cout << s << std::endl;
    return 0;
}

main();
 Welcome to MCA Department
tnemtrapeD ACM ot emocleW