Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reverse the string word
#2
The code assesses the sentence from the end to the beginning. The first while loop checks each character starting from the end until it finds a space. Once it finds a space, it iterates from the index start until index end and writes the word to the result. Then, it continues. The last loop basically does the same thing as the first one to pick up the first word in the original sentence.

Now, this can be written more concisely in Python.

def wordReverse(str): 
    words = str.split() # Split sentence by spaces into a list
    return " ".join(words[::-1])
Reply


Messages In This Thread
Reverse the string word - by sneha - Dec-10-2019, 03:00 PM
RE: Reverse the string word - by stullis - Dec-10-2019, 05:00 PM
RE: Reverse the string word - by sneha - Dec-12-2019, 03:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve word from string knob 4 523 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  extract substring from a string before a word !! evilcode1 3 570 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Isolate a word from a long string nicocorico 2 1,562 Feb-25-2022, 01:12 PM
Last Post: nicocorico
  printing a string in reverse Skaperen 2 1,574 Nov-20-2021, 05:08 AM
Last Post: ghoul
  I am trying to reverse a string using loop codinglearner 4 2,194 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  change string in MS word Mr_Blue 8 3,403 Sep-19-2021, 02:13 PM
Last Post: snippsat
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,538 Aug-12-2021, 04:25 PM
Last Post: palladium
  Reverse a String ragav_in 3 2,216 Jul-24-2020, 02:24 AM
Last Post: ragav_in
  Reverse word xSphere 6 3,790 Jul-02-2020, 04:10 PM
Last Post: pyzyx3qwerty
  Python Speech recognition, word by word AceScottie 6 16,065 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020