Emulating a do-while loop in Bash

Two simple solutions: Execute your code once before the while loopactions() { check_if_file_present # Do other stuff } actions #1st execution while [ current_time <= $cutoff ]; do actions # Loop execution done Or:while : ; do actions [[ current_time <= $cutoff ]] || break done

Batch script loop

for /l is your friend: Starts at 1, steps by one, and finishes at 100. Use two %s if it’s in a batch file (which is one of the things I really really hate about windows scripting) If you have multiple commands for each iteration of the loop, do this: or in a batch file Key:/l denotes that … Read more

Java compressing Strings

I need to create a method that receives a String and also returns a String. Ex input: AAABBBBCC Ex output: 3A4B2C Well, this is quite embarrassing and I couldn’t manage to do it on the interview that I had today ( I was applying for a Junior position ), now, trying at home I made … Read more

Correct way of looping through C++ arrays

In C/C++ sizeof. always gives the number of bytes in the entire object, and arrays are treated as one object. Note: sizeof a pointer–to the first element of an array or to a single object–gives the size of the pointer, not the object(s) pointed to. Either way, sizeof does not give the number of elements in the array (its length). To get the … Read more

How do I apply the for-each loop to every character in a String?

The easiest way to for-each every char in a String is to use toCharArray(): This gives you the conciseness of for-each construct, but unfortunately String (which is immutable) must perform a defensive copy to generate the char[] (which is mutable), so there is some cost penalty. From the documentation: [toCharArray() returns] a newly allocated character array whose length is the length of this string and whose contents are … Read more