I'm trying to write a program that reads a text from a file (only strings) and reverse it. The following code does that, but it doesn't take into account spaces between words:
#include<iostream>#include<vector>#include<fstream>using namespace std; int main(){ ifstream file("file.txt"); char i; int x; vector<char> vec; if(file.fail()){ cerr<<"error"<<endl; exit(1); } while(file>>i){ vec.push_back(i); } x=vec.size(); reverse(vec.begin(), vec.end()); for(int y=0; y<x; y++){ cout<<vec[y]; } return 0;}
If the text on the file is "dlroW olleH", the program would print out "HelloWorld". What can I do so that it prints "Hello World" (with the space between both words)?
I have this function here that will print a string until it gets to the last character. The thing is I need it to do the opposite of that.
Instead of 'abc' printing 'abc, ab, c' I need it to print 'c, bc, abc'.
Thank you!
def reverse(string): if len(string) == 0: return string else: print(string) return reverse(string[1:])
I have two strings. The first is normal string, the second I want to be a reversed string like first one, but in the console I didn't get the look of like first one listed by commas. How can I fix that ?
window.onload = function(){inputBox = document.getElementById("myText");btn = document.getElementById('sub');btn.addEventListener("click",function(event){event.preventDefault(); findPalindromes(inputBox.value); }); str = inputBox.value; function findPalindromes(str) { var words = str.split(" "); for (var i = 0; i < words.length - 1; i++) { words[i] += " "; } console.log(words); var newString = ""; for (var i = words.length - 1; i >= 0; i--) { newString += words[i]; } console.log(newString); }}
So I got stuck on a coding challenge that I almost knew the answer too. And I think I have to use the subString call in Swift 4 to get it 100%. I want to reverse every OTHER word in a string, but ignore or keep the punctuation in its original place( index ).
var sample = "lets start. And not worry about proper sentences."func reverseString(inputString: String) -> String { let oldSentence = sample.components(separatedBy: " ") var newSentence = "" for index in 0...oldSentence.count - 1 { let word = oldSentence[index] if newSentence != "" { newSentence += " " } if index % 2 == 1 { let reverseWord = String(word.reversed()) newSentence += reverseWord } else { newSentence += word } } return newSentence}reverseString(inputString: sample)
And this would be the expected output.
"lets trats. And ton worry tuoba proper secnetnes."
Notice the punctuation is not reversed.
My Task is to build a programm that takes a file as an argument and an optional argument that determines if the file will be outputted in reversed order. If the optional argument is "-r" then the content of the file will reverse when outputted.
#include <fstream>#include <iostream>#include <string>#include <algorithm>using std::cout;using std::string;using std::ios;using std::ifstream;using std::exception;int main(int argc,char *argv[]){ if (argc >= 2) { string fileContent; try { ifstream stream(argv[1]); fileContent = string( (std::istreambuf_iterator<char>(stream) ), (std::istreambuf_iterator<char>() ) ); } catch (const exception&) { return 0; } if (argc == 3 && strcmp(argv[2],"-r") == 0){ std::reverse(fileContent.begin(),fileContent.end()); } cout << fileContent; } return 0;}
This is my Python implementation of this problem. I ouputted the result in a new file and checked it with the solution we recived.(Checked if the hashes of both output files match). I noticted that Python's string reverse will not reverse '\r\n'.I do not think that there is an issues with the encoding of the file which should be utf-8.
import sysif len(sys.argv) >= 2: with open(sys.argv[1],"r") as f: string = str(f.read()) print(string[::-1] if len(sys.argv) == 3 and sys.argv[2] == "-r" else string,end="")
Please note that by viewing our site you agree to our use of cookies (see Privacy for details). You will only see this message once.