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

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

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&lt;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&lt;length;i++)
        {
            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;)
            count++;
        }
        int c=length-count;
        int product = c * count;
        System.out.printf(&quot;%d %d %d&quot;,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(&quot;Enter your number : &quot;);
        int n = sc.nextInt();
        for (int i = 0; i &lt; n; i++) {
            System.out.println(&quot;Enter your name : &quot;);
            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 &lt; str.length(); i++) {
            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;) {
                vCount++;
            } else if (str.charAt(i) &gt;= &#39;a&#39; &amp;&amp; str.charAt(i) &lt;= &#39;z&#39;) {
                cCount++;
            }
        }
        System.out.println(&quot;Number of vowels: &quot; + vCount);
        System.out.println(&quot;Number of consonants: &quot; + cCount);
        System.out.println(&quot;Product of vowels and consonants :&quot; + vCount * cCount);
    }
}

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:

确定