简体   繁体   English

从用户处获取一个字符,并在 Java 中用户给出的字符串中查找该字符的出现次数

[英]Get a character from the user and find the no of occurrences of the character in a string given by the user in Java

Count the no of occurrences of the given character.计算给定字符的出现次数。 Write a program to accept a word from the user.编写一个程序来接受用户的一句话。 Get a character from the user and find the no of occurrences.从用户那里获取一个字符并找到出现次数。

Check whether the given character and word is valid检查给定的字符和单词是否有效

The word is valid if it contains only alphabets and no space or any special characters or numbers.如果单词仅包含字母且不包含空格或任何特殊字符或数字,则该单词是有效的。

The character is valid if it is an alphabet alone.如果字符是单独的字母,则该字符是有效的。

Sample Input 1: Enter a word: programming Enter a character: m示例输入 1:输入一个单词:编程输入一个字符:m

Sample Output 1:样品 Output 1:

No of 'm' present in the given word is 2

Sample Input 2: Enter a word: programming Enter the character: s示例输入 2:输入一个单词:programming 输入字符:s

Sample Output 2:样品 Output 2:

The given character 's' not present in the given word.

Sample Input 3: Enter a word: 56 Sample Output 3:示例输入 3:输入一个单词:56 示例 Output 3:

Not a valid string

Sample Input 4: Enter a word: Hello Enter the character: 6示例输入 4:输入单词:Hello 输入字符:6

Sample Output 4:样品 Output 4:

Given character is not an alphabet

My code:我的代码:

import java.util.Scanner;

public class OccurrenceOfChar {

    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        // Fill the code
        System.out.println("Enter a word:");
        String word=sc.nextLine();
        String words=word.toLowerCase();
        String find="";
        int len=word.length();
        int i, count=0, flag=0;
        for(i=0;i<len;i++)
        {
            char c=words.charAt(i);
            if(!(c>='a' && c<='z'))
            {
                System.out.println("Not a valid string");
                break;
            }
        }
        System.out.println("Enter the character:");
        find=sc.nextLine();
        if(!(find.length()==1) && (find.charAt(0)>='a' && (find.charAt(0)<='z')))
        {
            System.out.println("Given character is not an alphabet");
        }
        for(i=0;i<len;i++)
        {
            if(words.charAt(i)==find.charAt(0))
            {
                count++;
            }
        }
        if(count==0)
            System.out.println("The given character '"+find+"' not present in the given word.");
        else
            System.out.println("No of '"+find+"' present in the given word is "+count);
    }

   }

Only 2 of the 9 test cases passed. 9 个测试用例中只有 2 个通过。 I cannot point out the mistake in the logic.我无法指出逻辑中的错误。

Test 3: Check the logic when character is not present and word is in capital测试 3:检查不存在字符且单词大写时的逻辑

Test 4: Check the logic when character is present and word is in capital测试 4:检查字符存在且单词大写时的逻辑

Test 5: Check the logic when the word is invalid测试五:判断单词无效时的逻辑

Test 6: Check the logic when the character is invalid测试6:检查字符无效时的逻辑

Test 7: Check the logic when the character has 2 digits测试7:检查字符有2位时的逻辑

Test 8: Check the logic when the word has no alphabets测试 8:当单词没有字母时检查逻辑

Test 9: Check the logic when the character is a special character测试9:检查字符为特殊字符时的逻辑

*Note: All the test cases might not have same weightage +------------------------------+ | *注意:所有测试用例的权重可能不同 +--------------------------------------------+ | 9 tests run/ 2 tests passed | 9 次测试运行/2 次测试通过 | +------------------------------+ +------------------------------+

Move logic to validate string into separate method, call this method to check if given word is valid string将验证字符串的逻辑移动到单独的方法中,调用此方法来检查给定的单词是否为有效字符串

public boolean validateString(String str) {
      str = str.toLowerCase();
      char[] charArray = str.toCharArray();
      for (int i = 0; i < charArray.length; i++) {
         char ch = charArray[i];
         if (!(ch >= 'a' && ch <= 'z')) {
            return false;
        }
      }
      return true;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从用户输入的字符串句子中获取最大出现字符及其出现次数? - How to get the maximum occurring character and its occurrences from a user inputted String sentence? 使用Java Regex删除字符串开头的给定字符序列的出现次数 - Remove occurrences of a given character sequence at the beginning of a string using Java Regex "计算字符串中的字符出现次数 - Java" - Counting character occurrences in a string - Java 在Java中计算字符串中的字符出现次数 - count character occurrences in a string in Java 从java中的给定字符提取/截断字符串 - STRING EXTRACTION/TRUNCATE from a given character in java 如何在Java中打印给定字符串中的字符、数字和特殊字符? - How to print character, Number and special character from given String in Java? 如何从命令行参数计算字符串中的字符出现次数 - Java - How to count character occurrences in a string from command line arguments - Java 查找字符串中字符多次出现的位置 - Find Positions of Multiple Occurrences of a Character in a String 递归查找指定字符在字符串中出现的次数 - Recursion to find the number of occurrences of a specified character in a string 如何获取用户在 java 中输入的字符串的第一个字符 - How can I obtain the first character of a string that is given by a user input in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM