Asked by yourmom
Write a program that reads in a line of text and prints out the line of text backwards.
Line: This is an example line of text.
.txet fo enil elpmaxe na si sihT
ā
Here's another example:
Line: HeLLo ThErE!
!ErEhT oLLeH
All Answers 1
Answered by
GPT-5 mini
AI
Here are a few short programs that read one line and print it reversed.
Python (very short)
Python handles this with slicing:
```
line = input()
print(line[::-1])
```
C
Reads with fgets, strips trailing newline, and prints characters in reverse:
```
#include <stdio.h>
#include <string.h>
int main(void) {
char line[1000];
if (!fgets(line, sizeof line, stdin)) return 0;
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[--len] = '\0';
for (size_t i = len; i > 0; ) putchar(line[--i]);
putchar('\n');
return 0;
}
```
C++
Using std::getline and std::reverse:
```
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string line;
if (!std::getline(std::cin, line)) return 0;
std::reverse(line.begin(), line.end());
std::cout << line << '\n';
return 0;
}
```
Java
Using BufferedReader and StringBuilder:
```
import java.io.*;
public class ReverseLine {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
if (line != null) {
System.out.println(new StringBuilder(line).reverse().toString());
}
}
}
```
Node.js (JavaScript)
Using the readline module:
```
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', (line) => {
console.log(line.split('').reverse().join(''));
rl.close();
});
```
Examples
Input:
This is an example line of text.
Output:
.txet fo enil elpmaxe na si sihT
Input:
HeLLo ThErE!
Output:
!ErEhT oLLeH
Note: These simple reversals operate on code points/bytes. For full grapheme-aware Unicode reversal (so combined characters or emoji sequences stay intact) you need a Unicode-aware grapheme splitter library.
Python (very short)
Python handles this with slicing:
```
line = input()
print(line[::-1])
```
C
Reads with fgets, strips trailing newline, and prints characters in reverse:
```
#include <stdio.h>
#include <string.h>
int main(void) {
char line[1000];
if (!fgets(line, sizeof line, stdin)) return 0;
size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[--len] = '\0';
for (size_t i = len; i > 0; ) putchar(line[--i]);
putchar('\n');
return 0;
}
```
C++
Using std::getline and std::reverse:
```
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string line;
if (!std::getline(std::cin, line)) return 0;
std::reverse(line.begin(), line.end());
std::cout << line << '\n';
return 0;
}
```
Java
Using BufferedReader and StringBuilder:
```
import java.io.*;
public class ReverseLine {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
if (line != null) {
System.out.println(new StringBuilder(line).reverse().toString());
}
}
}
```
Node.js (JavaScript)
Using the readline module:
```
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', (line) => {
console.log(line.split('').reverse().join(''));
rl.close();
});
```
Examples
Input:
This is an example line of text.
Output:
.txet fo enil elpmaxe na si sihT
Input:
HeLLo ThErE!
Output:
!ErEhT oLLeH
Note: These simple reversals operate on code points/bytes. For full grapheme-aware Unicode reversal (so combined characters or emoji sequences stay intact) you need a Unicode-aware grapheme splitter library.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.