统计元音和辅音的数量,并将它们的数量和乘积打印在一个字符串中。

huangapple 未分类评论45阅读模式
英文:

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

以下是代码-

  1. import java.io.*;
  2. import java.util.*;
  3. public class CandidateCode {
  4. public static void main(String args[] ) throws Exception {
  5. Scanner sc=new Scanner(System.in);
  6. int n=sc.nextInt();
  7. for(int i=0;i<n;i++)
  8. {
  9. String s=sc.nextLine();
  10. alpha(s);
  11. System.out.println();
  12. }
  13. }
  14. private static void alpha(String s)
  15. {
  16. int count=0;
  17. int length=s.length();
  18. for(int i=0;i<length;i++)
  19. {
  20. if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
  21. count++;
  22. }
  23. int c=length-count;
  24. int product = c * count;
  25. System.out.printf("%d %d %d",count,c,product);
  26. }
  27. }
英文:

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-

  1. import java.io.*;
  2. import java.util.*;
  3. public class CandidateCode {
  4. public static void main(String args[] ) throws Exception {
  5. Scanner sc=new Scanner(System.in);
  6. int n=sc.nextInt();
  7. for(int i=0;i&lt;n;i++)
  8. {
  9. String s=sc.nextLine();
  10. alpha(s);
  11. System.out.println();
  12. }
  13. }
  14. private static void alpha(String s)
  15. {
  16. int count=0;
  17. int length=s.length();
  18. for(int i=0;i&lt;length;i++)
  19. {
  20. if(s.charAt(i)==&#39;a&#39; || s.charAt(i)==&#39;e&#39; || s.charAt(i)==&#39;i&#39; || s.charAt(i)==&#39;o&#39; || s.charAt(i)==&#39;u&#39;)
  21. count++;
  22. }
  23. int c=length-count;
  24. int product = c * count;
  25. System.out.printf(&quot;%d %d %d&quot;,count,c,product);
  26. }
  27. }

答案1

得分: 0

  1. public class CandidateCode {
  2. public static void main(String args[]) throws Exception {
  3. Scanner sc = new Scanner(System.in);
  4. System.out.print("Enter your number : ");
  5. int n = sc.nextInt();
  6. for (int i = 0; i < n; i++) {
  7. System.out.println("Enter your name : ");
  8. String s = sc.next();
  9. alpha(s);
  10. }
  11. }
  12. private static void alpha(String str) {
  13. int vCount = 0, cCount = 0;
  14. str = str.toLowerCase();
  15. for (int i = 0; i < str.length(); i++) {
  16. if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
  17. vCount++;
  18. } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
  19. cCount++;
  20. }
  21. }
  22. System.out.println("Number of vowels: " + vCount);
  23. System.out.println("Number of consonants: " + cCount);
  24. System.out.println("Product of vowels and consonants :" + vCount * cCount);
  25. }
  26. }
英文:
  1. public class CandidateCode {
  2. public static void main(String args[]) throws Exception {
  3. Scanner sc = new Scanner(System.in);
  4. System.out.print(&quot;Enter your number : &quot;);
  5. int n = sc.nextInt();
  6. for (int i = 0; i &lt; n; i++) {
  7. System.out.println(&quot;Enter your name : &quot;);
  8. String s = sc.next();
  9. alpha(s);
  10. }
  11. }
  12. private static void alpha(String str) {
  13. int vCount = 0, cCount = 0;
  14. str = str.toLowerCase();
  15. for (int i = 0; i &lt; str.length(); i++) {
  16. if (str.charAt(i) == &#39;a&#39; || str.charAt(i) == &#39;e&#39; || str.charAt(i) == &#39;i&#39; || str.charAt(i) == &#39;o&#39; || str.charAt(i) == &#39;u&#39;) {
  17. vCount++;
  18. } else if (str.charAt(i) &gt;= &#39;a&#39; &amp;&amp; str.charAt(i) &lt;= &#39;z&#39;) {
  19. cCount++;
  20. }
  21. }
  22. System.out.println(&quot;Number of vowels: &quot; + vCount);
  23. System.out.println(&quot;Number of consonants: &quot; + cCount);
  24. System.out.println(&quot;Product of vowels and consonants :&quot; + vCount * cCount);
  25. }
  26. }

huangapple
  • 本文由 发表于 2020年4月8日 16:45:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61096692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定