The program takes a string and creates a dictionary with key as first character and value as words starting with that character.
1. Enter a string and store it in a variable.
2. Declare an empty dictionary.
3. Split the string into words and store it in a list.
4. Using a for loop and if statement check if the word already present as a key in the dictionary.
5. If it is not present, initialize the letter of the word as the key and the word as the value and append it to a sublist created in the list.
6. If it is present, add the word as the value to the corresponding sublist.
7. Print the final dictionary.
test_string=input("Enter string: ")
for word in l:
if word not in d.keys():
if word not in d[word]:
for k,v in d.items():
1. User must enter a string and store it in a variable.
2. An empty dictionary is declared.
3. The string is split into words and is stored in a list.
4. A for loop is used to traverse through the words in the list.
5. An if statement is used to check if the word already present as a key in the dictionary.
6. If it is not present, the letter of the word is initialized as the key and the word as the value and they are appended to a sublist created in the list.
7. If it is present, the word is added as the value to the corresponding sublist.
8. The final dictionary is printed.
Runtime Test Cases:
Enter string: Hello world this is a test string
'a' : ['a']
'i' : ['is']
'H' : ['Hello']
's' : ['sanfoundry', 'string']
't' : ['test', 'this']
'w' : ['world']
Enter string: python is my most favourite programming language in the entire world
'e' : ['entire']
'f' : ['favourite']
'i' : ['in', 'is']
'm' : ['most', 'my']
'l' : ['language']
'p' : ['programming', 'python']
't' : ['the']
'w' : ['world']