Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,094 questions

40,775 answers

573 users

How to reverse the middle words of a string in JavaScript

1 Answer

0 votes
function reverse_string(s) {
    return s.split("").reverse().join("");
}
  
function reverse_middle_words(s) { 
    var len = s.length;
    var i = 0; 
    var tmp = "";
     
    for (; i < len && s[i] !== ' '; i++) {
        tmp += s[i]; 
    }
 
    word = ""; 
    for (; i < len; i++) { 
        if (s[i] !== ' ') {
            word = word + s[i]; 
        }
        else { 
            tmp += reverse_string(word) + " "; 
            word = ""; 
        } 
    } 
   
    tmp += word;
     
    return tmp;
} 
   
 
 
s = "c# javascript java python php"; 
 
s = reverse_middle_words(s); 
 
document.write(s);
    
          
    
    
/*
run:
     
c# tpircsavaj avaj nohtyp php 
    
*/

 





answered Dec 13, 2019 by avibootz

Related questions

1 answer 89 views
1 answer 110 views
1 answer 79 views
1 answer 88 views
...