Friday, 29 April 2011

python program for reversing the position of the words in a line

This is a program for reversing the position of the words in a line

the code is

f = open('words.txt')
iamlist = []
while True:
    line = f.readline()
    if len(line) == 0 :
        break
    else:
        iamlist = line.rstrip('\n').split(" ")
        iamlist.reverse()
        print " ".join(iamlist)

f.close()

i/p(words.txt):

this is a text string
this is another string

o/p:

[parthi@localhost pyth files]$ python reversewords.py
string text a is this
string another is this


No comments:

Post a Comment