Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what will be the output of the following code ?

–1 vote
asked Oct 20, 2019 by Aditya Jain (110 points)
s=input('enter the string  ')

k=len(s)

a=''

p=''

for i in range(k):
  
       a=a+s[i]
 
       if(s[i]==' ' or i==k-1):
 
       t=a[::-1]
 
       p=p+t
    
       a=''

print(p)

5 Answers

0 votes
answered Oct 21, 2019 by anonymous
say if the string is 'Aman'

so the output willl be 'nama'

this is just a simple reverse of string

but why to write such a big code when just 1 line of code can give you the same answer.

use ::-1
commented Oct 21, 2019 by Aditya Jain (110 points)
bro, do you actually have the knowledge of coding.Maybe you are not able to get what i have written in the code. This is not simple reverse string problem. You should first carefully read the code then comment.Good Luck
0 votes
answered Oct 22, 2019 by Molero Raffi (540 points)

This code is not valid. This if block:

if(s[i]==' ' or i==k-1):
t=a[::-1]
p=p+t
a=''

is not indented.

The interpreter does not see what the if statement should do, and outputs this:

    File "main.py", line 8
        t=a[::-1]
        ^
IndentationError: expected an indented block

This means that your code doesn't output anything.

However, if I change the code to:

s=input('enter the string  ')
k=len(s)
a=''
p=''
for i in range(k):
    a=a+s[i]
    if(s[i]==' ' or i==k-1):
        t=a[::-1]
        p=p+t
        a=''
print(p)

then it becomes valid.

This program outputs the input but with each word reversed in the sentence.

But, there's a twist.
For example:
if you input 'This is a sentence.',
it outputs ' siht si a.ecentnes'

Notice that there is a spacebar before the 'siht' but not before the '.ecentnes'.

This is because the program includes the spacebar when reversing, not just the word itself.

So, that's what it does.

commented Oct 22, 2019 by Arun Chauhan (100 points)
there is indented if we fix it so output is reverse of string..

s=input('enter the string  ')

k=len(s)

a=''

p=''

for i in range(k):
  
       a=a+s[i]
 
       if(s[i]==' ' or i==k-1):
 
                 t=a[::-1]
 
                 p=p+t
    
                 a=''

                 print(p)




input=arun
output=nura
0 votes
answered Oct 23, 2019 by anonymous
identation error
0 votes
answered Oct 23, 2019 by anonymous
After giving indentation after if block. It prints reverse of string
0 votes
answered Nov 7, 2019 by dheeraj (1,090 points)
enter the string  ramu
umar

it is giving like this

it is just printing what we give as string in reverse
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...