Write a Pascal Program/ Python program to count each Alphabets in a given String and print it as given below. Example: (Output Screen)
Pascal Program for Count Alphabets in String program CountAlphabets; { Define Variables } var s:String; i,x:integer; mychar: array[1..26] of integer; begin {enter the name} Write('Enter Your full Name?'); readln(s); s:=UpCase(s); {set array elements to 0} for i:=1 to 26 do begin mychar[i]:=0; end; {count characters} for i:=1 to length(s) do begin x:=(ord(s[i])-64); if (x > =1) and (x < =26) then begin mychar[x]:=mychar[x]+1; end; end; {Display count charters with values} for i:=1 to 26 do begin write(chr(i+64)+ ' : '); writeln(mychar[i]); end; readln; end.
Python program is given below with a user defined function to the above problem.
def char_search(n): string = n string=string.upper() for c in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'): char = c val = string.count(char) print(c,'Total = ',val) yourname=input('Enter you Name?') char_search(yourname)
Another way to write Python Program
n=input("Enter a Name") c=list() for x in range(0,26): c.append(0) for x in n: x=x.upper() if (ord(x)>=65 and ord(x)<=91): c[ord(x)-65]=c[ord(x)-65]+1 for l in range(0,26): print(chr(l+65),"-",c[l])
No comments:
Post a Comment