In: Computer Science
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions.
For character identification
Develop a program that takes a string argument, and returns an array containing all unique characters.
For character counting
Develop a program that takes an array returned from above function as an argument along with the given string and return an array containing the total count of each uniquely identified character present in the argument array.
Sol:
Here is a python code for above problem with self-explanatory comments:
#creating a count function that takes unique chracters and input string as passed arguments
def count(unq,x):
temp=0
for i in sorted(unq):
temp=0
for j in sorted(x):
if i==j:
temp+=1
print("Count of "+i+" is: "+str(temp))
#creating unique function that takes input string x as argument and returns al the distinct characters
def unique(x):
k=[]
for i in x:
#only storing unique characters in our array
if i not in k:
k.append(i)
return k
#taking string input
x=input("Enter your string input here:")
#calling unique function which will return all the unique characters
unq=unique(x)
print("Unique characters are: ")
print(unq)
#calling count function that will return count of each character
cnt=count(unq,x)
Sample Output: