Skip to content

Instantly share code, notes, and snippets.

@jeandudey
Created July 25, 2015 14:48
Show Gist options
  • Save jeandudey/f6b5d0b9324fff8a10d0 to your computer and use it in GitHub Desktop.
Save jeandudey/f6b5d0b9324fff8a10d0 to your computer and use it in GitHub Desktop.
Backward Text on C++.

BackwardText

Reads a text file and display it backwards in the console.

Compile

g++ main.cpp -o backward.exe

#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char **argv)
{
std::ifstream file("HelloWorld.txt");
std::string words;
if (file.is_open()) {
std::string temp;
while (std::getline(file, temp)) {
words += temp;
words.push_back('\n');
}
}
for(int i = 0; i < words.length() / 2; ++i) {
char temp = words[i];
words[i] = words[words.length() - (i + 1)];
words[words.length() - (i + 1)] = temp;
}
std::cout<<words<<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment