Hisham Souraya
Hisham Souraya

Reputation: 29

How to count specific occurrences and letters from a string by creating own method in java

I need to create a method that counts the number of occurrences that happens in string str using string chars and I need to pass these tests that is my assignment so not my code:

@Test
    public void test_count_bothEmptyString() {
        int expected = 0;
        int actual = CCStringsIfAndWhile.count("", "");
        
        assertEquals("Testing count - both empty string", expected, actual);
    }

    @Test
    public void test_count_firstEmptyString() {
        int expected = 0;
        int actual = CCStringsIfAndWhile.count("", "abcdefghijklmnopqrstuvwxyz");
        
        assertEquals("Testing count - first is empty string", expected, actual);
    }

    @Test
    public void test_count_secondEmptyString() {
        int expected = 0;
        int actual = CCStringsIfAndWhile.count("This is a test", "");
        
        assertEquals("Testing count - second is empty string", expected, actual);
    }

    @Test
    public void test_count_one() {
        int expected = 1;
        int actual = CCStringsIfAndWhile.count("This is a test", "abc");
        
        assertEquals("Testing count - 'This is a test', 'abc'", expected, actual);
    }

    @Test
    public void test_count_many() {
        int expected = 6;
        int actual = CCStringsIfAndWhile.count("This is a test", "sapqi");
        
        assertEquals("Testing count - 'This is a test', 'sapqi'", expected, actual);
    }

    @Test
    public void test_count_upperAndLowerCase() {
        int expected = 7;
        int actual = CCStringsIfAndWhile.count("This is another test", "stpq");
        
        assertEquals("Testing count - 'This is another test', 'stpq'", expected, actual);
    }

I've tried something like this however I can't seem to find an if statement that checks each letter of the string:

public static int count(String str, String chars) 
    {
        int count = 0;
        int charCount = 0;
        while(count<str.length()) 
      {
            if() 
            {
                charCount++;
                return charCount++;
            }

       else {
                charCount=0;
                
            }

            count++;
        }
        return charCount;
        
    }

Upvotes: 0

Views: 286

Answers (3)

Old Dog Programmer
Old Dog Programmer

Reputation: 1250

You will find methods of the String API useful.

Here is one possible solution:

 public static int count (String str, String chars) {
     int matchCount = 0;
     for (int idx = 0; idx < str.length (); ++i) {
        if (chars.indexOf(str.charAt(idx)) >= 0) { ++matchCount; }
     }
     return matchCount;
 }

If chars is empty, this will waste time stepping through each character of str. It would be helpful to add a guard if in front of the for loop:

    if (chars.isEmpty()) { return 0; } 

 

Upvotes: 0

Hisham Souraya
Hisham Souraya

Reputation: 29

This is what I did and it worked thank you for the help:

    public static int count(String str, String chars) 
    {

        int charCount = 0;
        if(str.isEmpty()||chars.isEmpty()) 
        {
            return 0;
            
        }
        
        for (char strC: str.toCharArray()) 
        {
          for (char charsC: chars.toCharArray()) 
          {
             if(Character.toLowerCase(strC)==Character.toLowerCase(charsC)) 
             {
                charCount++;

             }
          }
        }
        return charCount;

Upvotes: 1

Icarus
Icarus

Reputation: 511

  1. should probably use two loops since you need to iterate through both string to find all occurances of chars of one string in another

  2. return charCount++; definitely not right, the return inside the loop will stop the loop prematurely

  3. judging by your test cases you will need to add an if statement to check for empty string in either inputs and return a 0

Implementing above you get sth like this

 if(str.isEmpty()||chars.isEmpty()){return 0;}
 for (char strChar: str.toCharArray()) {
   for (char chars: chars.toCharArray()) {
      if(strChar==chars){
         charCount++;
      }
   }
 }

Upvotes: 1

Related Questions