英文:
count no. of vowels, consonants, and print their count and product in a string
问题
以下是翻译好的内容:
在问题中,我们被给予两个字符串,我们需要计算每个字符串中的元音数量、辅音数量,并分别显示它们的乘积。
期望输出
2 6 12
0 7 0
实际输出
0 0 0
2 6 12
以下是代码-
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String s=sc.nextLine();
alpha(s);
System.out.println();
}
}
private static void alpha(String s)
{
int count=0;
int length=s.length();
for(int i=0;i<length;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
count++;
}
int c=length-count;
int product = c * count;
System.out.printf("%d %d %d",count,c,product);
}
}
英文:
In the Question we are given 2 strings and we have to count no. of vowels, no. of consonants and display their product individually for each string.
Expected Output
2 6 12
0 7 0
Output getting
0 0 0
2 6 12
Here is the code-
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String s=sc.nextLine();
alpha(s);
System.out.println();
}
}
private static void alpha(String s)
{
int count=0;
int length=s.length();
for(int i=0;i<length;i++)
{
if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
count++;
}
int c=length-count;
int product = c * count;
System.out.printf("%d %d %d",count,c,product);
}
}
答案1
得分: 0
public class CandidateCode {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number : ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("Enter your name : ");
String s = sc.next();
alpha(s);
}
}
private static void alpha(String str) {
int vCount = 0, cCount = 0;
str = str.toLowerCase();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
vCount++;
} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
System.out.println("Product of vowels and consonants :" + vCount * cCount);
}
}
英文:
public class CandidateCode {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number : ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("Enter your name : ");
String s = sc.next();
alpha(s);
}
}
private static void alpha(String str) {
int vCount = 0, cCount = 0;
str = str.toLowerCase();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
vCount++;
} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
System.out.println("Product of vowels and consonants :" + vCount * cCount);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论