EXERCISE - 5 DATA STRUCTURES

5 A) Write a program to count the number of characters in the string and store them in a dictionary data structure.
B) Write a program to use split and join methods in the string and trace a birthday with a dictionary data structure. A) AIM: Write a program to count the number of characters in the string and store them in a dictionary data structure.
SOURCE CODE:
str1=input("Enter String:\n") dict1={} for ch in str1: keys=dict1.keys() if(ch in keys): dict1[ch]=dict1[ch]+1 else: dict1[ch]=1 print("Dictionary is:\n",dict1) OUTPUT:
Enter String: hello world Dictionary is: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1} B) AIM: Write a program to use split and join methods in the string and trace a birthday with a dictionary data structure.
SOURCE CODE:
bdaystr=input("Enter date of birth:\n") bdaylist=bdaystr.split("/") bday='-'.join(bdaylist) bdaydict={"birthday":bday} if 'birthday' in bdaydict: print(bdaydict['birthday']) OUTPUT:
Enter date of birth: 01/01/2017 01-01-2017

No comments:

Post a Comment

Total Pageviews