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="")
I'm trying to make a method that will take an array of strings and return an array with all the words in reverse order but my code is not working.
When I run it i get "[Ljava.lang.String;@74122d9c"
Test case: String[]words={"Monday","Tuesday","Wednesday"}; -->{"yadnoM","yadsueT","yadsendeW"};
public String[] reverseString(String[] words){ String[] t=new String[words.length]; for(int i=0;i<words.length;i++) { for(int j=words[i].length()-1;j>=0;j--) { t[i]+=words[i].substring(j,j+1); } } return t;}
منزل - خريطة الموقع - خصوصية - وصلات - حقوق التأليف والنشر © 2018 اللحاء تكنولوجيا المعلومات المحدودة : اتصال : admin @ cortexit.co.uk
Please note that by viewing our site you agree to our use of cookies (see خصوصية for details). You will only see this message once.