Lexicographic sorting
Aim: Implement
a program to illustrate how words can be sorted lexicographically (alphabetic
order).
Algorithm:
1.
Start.
2.
Read the
string .
3.
Breakdown the string into a list of
words using split() function.
4.
Sort the list using sort() function.
5.
Display the sorted words.
6.
Stop.
Program:
# take input from the
user
my_str =
input("Enter a string: ")
# breakdown the string
into a list of words
words = my_str.split()
# sort the list
words.sort()
# display the sorted
words
for word in words:
print(word)
Output:
Enter a string:
Hello this Is an Example With cased letters
Example
Hello
Is
With
an
cased
letters
this
0 Comments:
Post a Comment