Note: Due to the conditions clarified in the title, it seems to be a class exercise, so my answer focuses on not using modules, meeting the aforementioned conditions and being the most understandable for a low level of programming knowledge. Also, it assumes that each word is separated by spaces.
You can achieve it with the following code:
cadena = "esta es una cadena de prueba"
words = []
word = ""
for c in cadena:
if(c == " "):
words.append(word)
word = ""
else:
word = c + word
words.append(word)
print(" ".join(words))
The plan is to create a list of words that starts out empty and a variable word that will store the word that we are inverting. Next, we loop through each character in the string with a for loop. If the character is a space, we add the already inverted word to the list and reset the variable word to an empty string to start inverting the next word. Otherwise, we "append" the character to the beginning of word and store the newly created string in the word variable.
Once the entire chain has been traversed, we have one word left that has not been added to the list, so we add it.
Note: Due to the conditions clarified in the title, it seems to be a class exercise, so my answer focuses on not using modules, meeting the aforementioned conditions and being the most understandable for a low level of programming knowledge. Also, it assumes that each word is separated by spaces.
You can achieve it with the following code:
The plan is to create a list of words that starts out empty and a variable word that will store the word that we are inverting. Next, we loop through each character in the string with a for loop. If the character is a space, we add the already inverted word to the list and reset the variable word to an empty string to start inverting the next word. Otherwise, we "append" the character to the beginning of word and store the newly created string in the word variable.
Once the entire chain has been traversed, we have one word left that has not been added to the list, so we add it.