[英]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.