简体   繁体   中英

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

Sample Output 1:

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

Sample Input 2: Enter a word: programming Enter the character: s

Sample Output 2:

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

Sample Input 3: Enter a word: 56 Sample Output 3:

Not a valid string

Sample Input 4: Enter a word: Hello Enter the character: 6

Sample 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. I cannot point out the mistake in the logic.

Test 3: Check the logic when character is not present and word is in capital

Test 4: Check the logic when character is present and word is in capital

Test 5: Check the logic when the word is invalid

Test 6: Check the logic when the character is invalid

Test 7: Check the logic when the character has 2 digits

Test 8: Check the logic when the word has no alphabets

Test 9: Check the logic when the character is a special character

*Note: All the test cases might not have same weightage +------------------------------+ | 9 tests run/ 2 tests passed | +------------------------------+

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;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM