Experience: is what you get soon after you need it.

Experience: is what you get soon after you need it.

Rasul Allah (sal Allahu alaihi wa sallam) said: "Restore the trusts of those who trust you, and deal not falsely with him who deals falsely with you." [Abu Dawud, Tirmidhi]

Search This Blog

Thursday, November 19, 2015

Reverse string,words and check for palindrome in Python



cat words.py
####START OF SCRIPT#########
words1 = "test"
ask = "test"

def splitinput():
        global ask
        ask = raw_input("\nEnter few words here:\t")
        global words1
        words1 = ask.split(' ')
        return words1
        return ask

def reverse():
        global words1
        global ask
        j = ''
        for i in range(len(ask)-1,-1,-1):
                j = j + ask[i]
        print "\nPrinting the Reversed string:\t",j

def same():
        global ask
        j = ''
        for i in range(len(ask)):
                j = j + ask[i]
        print "\nPrinting in the same order\t%s" % j

words2 = ''
def reverseword():
        global words1
        global words2
        for i in range(len(words1)-1,-1,-1):
            words2 = words2 + words1[i] + ' '
        print "\nPrinting in the reverse order\t%s" % words2
        return words2

def palindrome():
        global words1
        global words2
        global ask
        for i in range(len(words1)):
                j = words1[i]
                l = ''
                for k in range(len(j)-1,-1,-1):
                        l = l + j[k]
                #print "print j[k]\t%s" % l
                if j == l:
                        print "\nWord """"%s"""" is a palindrome" % l


while True:
        splitinput()
        same()
        reverseword()
        reverse()
        palindrome()
        ask1 = raw_input("\nDo you wanna play again? Type Y|y|YES|yes:\t")
        if ask1 in ['Y', 'y', 'yes', 'YES']:
                print "Okay..Let's do it again"
        else:
                print "\nThanks for playing....Exiting!\n"
                exit(0)
####END OF SCRIPT#########


Output:
=====

shaiks@MAC$py words.py

Enter few words here:    Sameer is good boy and GOOG

Printing in the same order    Sameer is good boy and GOOG

Printing in the reverse order    GOOG and boy good is Sameer

Printing the Reversed string:    GOOG dna yob doog si reemaS

Word GOOG is a palindrome

Do you wanna play again? Type Y|y|YES|yes:    n

Thanks for playing....Exiting!
 

No comments: