Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Algorithm-a-Day : Day 1 : Reversing words in a string.

This algorithm takes a string and prints it out with the order
of the words flipped.
Appears to be a common interview question.
  • Loading branch information...
commit 443574f77714567beafcdb88178e6007e50b8647 0 parents
@fosskers authored
59 day1-reverse-words/reverse-words.c
@@ -0,0 +1,59 @@
+// Reverse the order of words in a string.
+
+#include <stdio.h>
+#include <string.h>
+
+void reverse_words(char *);
+void reverse(int, int, char *);
+
+int main(int argc, char *argv[]) {
+ int len;
+
+ if(argc > 1) {
+ reverse_words(argv[1]);
+ printf("%s\n", argv[1]);
+ }
+
+ return 0;
+}
+
+void reverse_words(char *line) {
+ // Detects where words start and begin, and calls reverse().
+ int word_start, word_end, in_word, count;
+ int lim = strlen(line);
+ char curr;
+
+ // Reverse individual words.
+ for(count = 0; count < lim; count++) {
+ curr = line[count];
+
+ if(!in_word && curr != ' ') {
+ in_word = 1;
+ word_start = count;
+ }
+ else if(in_word && curr == ' ') {
+ in_word = 0;
+ word_end = count - 1;
+ reverse(word_start, word_end, line);
+ }
+ else if(count == lim - 1)
+ reverse(word_start, count, line);
+ }
+
+ // Reverse entire string.
+ reverse(0, lim-1, line);
+}
+
+void reverse(int lower, int upper, char *line) {
+ // Reverses the chars in a string.
+ int lim = (upper + lower) / 2;
+ char swap;
+
+ for(lower = lower; lower <= lim; lower++) {
+ swap = line[lower];
+ line[lower] = line[upper];
+ line[upper] = swap;
+ upper--;
+ }
+}
+
4 day1-reverse-words/reverse-words.hs
@@ -0,0 +1,4 @@
+-- Flips the order of words in a string.
+
+reverseWords :: String -> String
+reverseWords = unwords . reverse . words
5 day1-reverse-words/reverse-words.py
@@ -0,0 +1,5 @@
+def reverse_words(line):
+ '''Reverses the order of words in a string.'''
+ words = items.split()
+ words.reverse()
+ return ' '.join(words)
Please sign in to comment.
Something went wrong with that request. Please try again.