+ 1

Calculate numbers of letters and digits

Write a program that accepts a sentence and calculate the number of letters and digits. Suppose the following input is supplied to the program: hello world! 123 Then, the output should be: LETTERS 10 DIGITS 3 ' ' ' use any language of your choice ' ' '

28th Apr 2018, 2:38 PM
Stanley Simon
Stanley Simon - avatar
5 Réponses
+ 3
😁😅 Thanks!
29th Apr 2018, 3:16 AM
Neha Mahadik
Neha Mahadik - avatar
+ 3
30th Apr 2018, 8:52 PM
Johannes
Johannes - avatar
+ 2
my code in Python s = raw_input() d={"DIGITS":0, "LETTERS":0} for c in s: if c.isdigit(): d["DIGITS;"]+=1 elif c.isalpha(): d["LETTERS;"]+=1 else: pass print ("LETTERS", d["LETTERS"]) print ("DIGITS", d["DIGITS"])
28th Apr 2018, 2:40 PM
Stanley Simon
Stanley Simon - avatar
+ 1
Program in Java: public static void main(String a[]) { scanner sc=new scanner(System.in); int digit=0, alpha=0; system.out.println("enter a sentence to calculate the number of letters and digits"); String s=sc.nextLine(); char a[]=s.toCharArray(); for (int i=0; i<a.length;i++) { if (a[i]>=65&&a[i]<=90||a[i]>=97&&a[i]<=122) // A-Z or a-z { alpha++; } if (a[i]>=48&&a[i]<=57) { digit++; } } system.out.println("LETTERS: "+alpha); system.out.println("DIGITS: "+digit); } P.S: This semester I had Java so tried in that. Your Python code is efficient..!
28th Apr 2018, 3:10 PM
Neha Mahadik
Neha Mahadik - avatar
28th Apr 2018, 4:56 PM
Timon Paßlick