DEV Community

Alex Wawl ๐Ÿผ
Alex Wawl ๐Ÿผ

Posted on

Devmates.co [Reverse Words In String]

Hello๐Ÿ‘‹,
My name is Alex and I'm maker of Devmates.co.
๐Ÿ˜Š

We are resolving daily coding problems which asked by top tech companies together. We share our solutions, give some help if anyone stuck, support each other and just hangout together in the internet. ๐Ÿปโ˜•๏ธ๐Ÿ’ฌ

I get this problem from our members or just searching them in the internet(leetcode forums, Glassdoor, teamblind, etc.). Sometimes this problem can be very easy, sometimes too ๐Ÿ‘ทโ€โ™‚๏ธhard, everything depends on skills but we are trying to resolve all of them and Have Fun. ๐Ÿ™Œ

Today I want to share problem which was asked by Bolt (๐Ÿš– Taxify).

Feel free to share you solutions in comments. I would love to find "better" solution and put it to the article.๐Ÿ˜‰

Problem:

Reverse each word in a given string.

You can't use language provided methods (such as split, reverse, etc.)

Example

__Input:__ 'hello this is devmates'
__Output:__ 'olleh siht si setamved'

Good Luck and Have Fun๐Ÿ˜‰

Top comments (1)

Collapse
 
alexwawl profile image
Alex Wawl ๐Ÿผ

I'll be the first.


function reverseWordsInString (phrase) {
          let result = "";
          let reversed_word = "";
          for (let i = 0; i < phrase.length; i++) {
              if (isSeparator(phrase[i])) {
                  result += reversed_word;
                  result += phrase[i];
                  reversed_word = "";
              } else {
                  reversed_word = phrase[i] + reversed_word;
              }
          }

          if (reversed_word.length > 0){
               result += reversed_word;
          }

          return result;
      }

      function isSeparator(symbol){
          switch (symbol) {
              case ' ':
                  return true;
              default:
                  return false;
          }
      }

Test Cases:


let reverseWordsInString = require('./someFile.js');


    test('Empty string', ()=>{
      expect(reverseWordsInString("")).toBe("")
    });

    test('One word', ()=>{
      expect(reverseWordsInString("hello")).toBe("olleh")
    });

    test('Valid string with single spaces', ()=>{
      expect(reverseWordsInString('hello this is devmates!')).toBe('olleh siht si !setamved')
    });

    test('Only spaces in string', ()=>{
      expect(reverseWordsInString("    ")).toBe("    ")
    });

    test('Multiply Spaces at the beginning in given string', ()=>{
      expect(reverseWordsInString("   hello this is devmates!")).toBe("   olleh siht si !setamved")
    });

    test('Multiply Spaces at the end in given string', ()=>{
      expect(reverseWordsInString("hello this is devmates!  ")).toBe("olleh siht si !setamved  ")
    });

    test('Multiply spaces at the beginning and at the end in given string', ()=>{
      expect(reverseWordsInString("  hello this is devmates!   ")).toBe("  olleh siht si !setamved   ")
    });

    test('Multiply spaces everywhere in given string', ()=>{
      expect(reverseWordsInString("   hello    this     is devmates  ")).toBe("   olleh    siht     si setamved  ")
    });