【问题标题】:Count the frequency of a Character in a Word计算单词中字符出现的频率
【发布时间】:2021-05-05 07:04:49
【问题描述】:

Raji 想计算给定字符出现的次数。编写一个程序来接受用户的一个单词。从用户那里获取一个字符并找到出现次数。

检查给定的字符和单词是否有效

如果单词仅包含字母且不包含空格或任何特殊字符或数字,则该单词有效。

如果字符是单独的字母,则该字符有效。

示例输入 1: 输入一个词: 编程 输入字符: 米

样本输出 1:

给定单词中出现的“m”个数是 2。

示例输入 2: 输入一个词: 编程 输入字符: s

示例输出 2:

给定的单词中不存在给定的字符's'。

示例输入 3: 输入一个词: 56 样本输出 3:

不是一个有效的字符串

示例输入 4: 输入一个词: 你好 输入字符: 6

示例输出 4:

给定的字符不是字母

我收到一个错误: 失败的测试: 测试3:检查不存在字符且单词大写时的逻辑

【问题讨论】:

  • 你尝试过什么,你在哪里卡住了?你能写什么代码?

标签: java string


【解决方案1】:

我认为这会奏效。虽然我无法通过一个测试用例,但请随意尝试它可能对您有用。

import java.util.*;
public class OccurrenceOfChar{
   public static void main (String[] args) {
          Scanner in = new Scanner(System.in);
          int n[] = new int[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},flag=0;
          System.out.println("Enter a word:");
          String input = in.nextLine();
          input=input.toLowerCase();
          for(int i=0;i<input.length();i++){
        if(((int)input.charAt(i)-97)<0 || ((int)input.charAt(i)-97)>25){
            flag=1;
        }
        else{
            n[(int)input.charAt(i)-97]++;
        }
    }
    if(flag==1){
        System.out.println("Not a valid string");
    }
    else{
        System.out.println("Enter the character:");
        String t = in.nextLine();
        String s=t.toLowerCase();
        if(((int)s.charAt(0)-97)<0 || ((int)s.charAt(0)-97)>25){
            System.out.println("Given character is not an alphabet");
        }
        else{
            if(n[(int)s.charAt(0)-97]>0){
                System.out.println("No of\'"+s+"\' present in the given word is "+n[(int)s.charAt(0)-97]+".");
            }
            else{
                System.out.println("The given character \'"+s+"\' not present in the given word.");
            }
        }
    }
}

}

【讨论】:

    【解决方案2】:
    import java.util.*;  
    import java.util.regex.Pattern;
    public class OccurrenceOfChar { 
    public static int count(String s, char c) 
    { 
        int res = 0; 
        for (int i=0; i<s.length(); i++) 
        { 
            if (s.charAt(i) == c) 
            res++; 
        } 
        return res; 
    } 
    
    public static void main(String args[]) 
    { 
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter a word: ");  
      
        String str= sc.nextLine();  
    if(Pattern.matches("^[a-zA-Z]*$",str) && (!str.isEmpty())){
        
        System.out.print("Enter the character: ");  
        char c = sc.next().charAt(0);
        
        if(Character.isAlphabetic(c)){
            
        if( str.indexOf(c)==-1){
            System.out.println("The given character '"+c+"' not present in the given word.");
        }else { 
        System.out.println("No of '"+c+ "' present in the given word is " +count(str, c)+"."); 
      }
       
        }
        else{
            System.out.println("Given character is not an alphabet");
        }
        
    }else{
        System.out.println("Not a valid string");
    }
    } 
    } 
    

    【讨论】:

      【解决方案3】:
      import java.util.Scanner;
      
      public class OccurrenceOfChar 
      
      {
      
          public static void main(String[] args) 
          { 
              Scanner sc = new Scanner(System.in); 
              System.out.println("Enter a word:"); 
              String word = sc.nextLine(); 
              char [] characterss = word.toCharArray(); 
              for(char ch:characterss)
              {
                  if(!Character.isLetter(ch)) 
                  {
                      System.out.println("Not a valid string"); 
                      System.exit(0);
                  }
              }
              System.out.println("Enter the character: "); 
              char ch = sc.nextLine().charAt(0);
      
              if(Character.isLetter(ch))
              {   
                  int count =0;
                  for(char chara:characterss)
                  {
                      if(chara == ch) 
                      {
                          count++;
                      }
                  }
                  if(count == 0)
                  {
                      System.out.println("The given character '"+ch+"' not present in the given word.");              
                  }
                  else
                  {
                      System.out.println("No of '"+ch+"' present in the given word is "+count);
                  }
              }
              else
              {
                  System.out.println("Given character is not an alphabet"); 
                  Systen.exit(0);
              }
          }
      }
      

      输出

      C:\Users\Dell\Desktop>java OccurrenceOfChar

      输入一个单词:

      程序员

      输入字符:

      给定单词中出现的“m”个数是 2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多