-1

I am doing the following programming exercise: The old switcheroo. The statement is:

Write a function

Kata.Vowel2Index("this is my string") == "th3s 6s my str15ng"

Kata.Vowel2Index("Codewars is the best site in the world") == "C2d4w6rs 10s th15 b18st s23t25 27n th32 w35rld"

Your function should be case insensitive to the vowels.

I have doubts when the input is an empty string, I mean when we test with:

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class KataTest {
  @Test public void test4() {
    assertEquals("", Kata.vowel2Index(""));
  }
  
}

My question is, why if we use contains, we have to check if input string is empty?

import java.util.*;
public class Kata {
  static final String VOWELS = "AEIOUaeiou";
  public static String vowel2Index/*➡️*/(String s) {
    if(s==null || s.equals("")) return "";
    String[] letters = s.split("");
    
    for(int i = 0; i < letters.length; i++){
      if(VOWELS.contains(letters[i])){
        letters[i] = String.valueOf(i+1);
      }
    }
    return String.join("",letters);
  }
}

Or check if the current string inside the loop is empty:

import java.util.*;
public class Kata {
  static final String VOWELS = "AEIOUaeiou";
  public static String vowel2Index/*➡️*/(String s) {
    String[] letters = s.split("");
    
    for(int i = 0; i < letters.length; i++){
      if(letters[i] != "" && VOWELS.contains(letters[i])){
        letters[i] = String.valueOf(i+1);
      }
    }
    return String.join("",letters);
  }
}

But we can omit the previous condition when we use indexOf:

import java.util.*;
public class Kata {
  static final String VOWELS = "AEIOUaeiou";
  public static String vowel2Index/*➡️*/(String s) {
    String[] letters = s.split("");
    
    for(int i = 0; i < letters.length; i++){
      if(VOWELS.indexOf(letters[i]) > 0){
        letters[i] = String.valueOf(i+1);
      }
    }
    return String.join("",letters);
  }
}

I have also read:

What are the differences between contains and indexOf?‽?

Community
  • 1
  • 1
Yone
  • 2,064
  • 5
  • 25
  • 56

3 Answers3

2
contains()

It is a common case in programming when you want to check if specific String contains a particular substring. For example, If you want to test if the String "The big red fox" contains the substring "red." This method is useful in such situation.

    String s1 = "My name is bar"; 
    System.out.println(s1.contains("bar")); 
    System.out.println(s1.contains("foo")); 

    //output
    true
    false

2..

indexOf()

This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.

    String gfg = new String("Welcome to stackoverflow"); 
    System.out.print("Found s first at position : ");  
    System.out.println(gfg.indexOf('s'));

   //output
   Found s first at position : 11
Jaydeep chatrola
  • 2,423
  • 11
  • 17
1

contains and indexOf behave exactly the same, they have the same runtime. contains is defined as indexOf(s.toString()) > -1.
In the corner case of an empty string "" as input to the methods they behave in a way you obviously do not expect: They return 0 / true. That means that your statements

VOWELS.contains(letters[i])
letters[i] != "" && VOWELS.contains(letters[i])
VOWELS.indexOf(letters[i]) > 0

simply do different things and are not equivalent.

For "" they are true, false, false.
For "A" they are true, true, false.
For "B" they are true, true, true.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • `indexOf(x) > 0` is *not* the same as `contains(x)`. I know it's a copy/paste error from the question, but you already showed the correct code, i.e. `indexOf(x) > -1`, or alternatively `indexOf(x) >= 0`. – Andreas Dec 27 '19 at 13:42
  • You don't actually say that anywhere, you don't explain why `"A"` behaves badly. --- Also, `letters[i] != ""` is bad code, since you can't use `!=` to compare strings. – Andreas Dec 27 '19 at 13:54
0

indexOf will return an Integer with the index where the substring beginns and -1 when the substring does not appear.

contains will return a boolean wether it contains the substring or not.

Kennuel
  • 169
  • 7
  • No, `contains` cannot be faster since it simply calls `indexOf`. – luk2302 Dec 27 '19 at 12:24
  • Just double checked - and you are right - I will update my answer. Back then, when the test was done it seemed that two different algorithms were used, – Kennuel Dec 27 '19 at 13:24