+ 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 ' ' '
5 Respostas
+ 3
😁😅 Thanks!
+ 3
My highly readable Python regex 5-liner:
https://code.sololearn.com/cSoufTbbN7U9/?ref=app
+ 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"])
+ 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..!