Reversal
2 minutes to read
We are given the following website to write a coding solution:
Problem
We need to write a program that is able to take a text string and print it in reverse order. For instance:
"asdf"
->"fdsa"
Solution
Since we are allowed to use C, C++, Python and Rust, let’s write a solution on every language, just because.
C
#include <stdio.h>
#include <string.h>
int main() {
// take in the string
char s[1024] = { 0 };
fgets(s, sizeof(s), stdin);
char* newline = NULL;
if ((newline = strchr(s, '\n'))) {
*newline = '\0';
}
// calculate answer
char answer[1024] = { 0 };
int length = strlen(s);
for (int i = 0; i < length; i++) {
answer[i] = s[length - i - 1];
}
// print answer
puts(answer);
return 0;
}
C++
#include <algorithm>
#include <iostream>
int main() {
// take in the string
std::string s;
getline(std::cin, s);
// calculate answer
std::string answer = s;
reverse(answer.begin(), answer.end());
// print answer
std::cout << answer << std::endl;
return 0;
}
Python
# take in the string
s = input()
# calculate answer
answer = s[::-1]
# print answer
print(answer)
Rust
use std::io;
fn main() {
// take in the string
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
// calculate answer
let answer = input.trim().chars().rev().collect::<String>();
// print answer
println!("{}", answer);
}
Flag
Using any of these programs, we are able to solve the chalenge and capture the flag:
HTB{r3VerS4l?_wElL_1_n3vEr_d0_th4t_th1ng}