Monday, February 18, 2019

Pascal and Python Program to Count each Alphabets in a given String

Write a Pascal Program/ Python program to count each Alphabets in a given String and print it as given below. Example: (Output Screen)


Pascal and Python Program to Count number Alphabets in given String
 
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)

No comments:

Post a Comment