Answers for "reverse each word"

0

reverse every word

function reverseWords(str) {
  const allWords = str.split(" ")
  return allWords.map(item => item.split("").reverse().join("")).join(" ")
}
Posted by: Guest on August-17-2020
0

reverse each word

string reverse(string &s)//this func reverses single words
    {
        string r;
        for(int i=s.size()-1;i>=0;i--)
        {
            r+=s[i];
        }
        return r;
    }
    //Function to reverse words in a given string.
    string reverseWords(string s) 
    { 
        // we are reversing eachword from the end adn adding
        string k,ans;
        for(int i=s.size()-1;i>=0;i--)
        {
            if(s[i]=='.')
            {
                k=reverse(k);
                ans+=k;ans+='.';
                k.clear();
                continue;
            }
            
            k+=s[i];
        }
        ans+=reverse(k);
        return ans;
    }
Posted by: Guest on August-16-2021

Browse Popular Code Answers by Language