Wednesday, 13 June 2018

Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically using Python Programming.

# Write a program that accepts a sequence of whitespace separated words as input and
# prints the words after removing all duplicate words and sorting them
# alphanumerically

str = input("Enter a whitespace seprated string : ")

if len(str)>0:
    sstr=""
    wordList = str.split(" ")
    wordList = sorted(wordList)
    for w in wordList:
        if sstr.count(w)==0:
            sstr +=w
            sstr +=" "
    sstr = sstr[:-1]
    print("Sorted String : ",sstr)
else:
    print("String is empty.")

input("Press Enter to exit.")

No comments:

Post a Comment

Popular Posts