Dino
Dino

Reputation: 561

Search string array without a using a loop

I have the following array

String[] arrKey  = new String[] {"A","B","C","D","E",......"Y","Z"};

I would like to search the array and give me the index of where the letter is. Say for example I want to search for the letter "E" and when I search the array it should give me the position of "E" so I should get index position 4. I don't want to do this in a loop. Is it possible? I have been looking all around and can't find an answer.

Upvotes: 3

Views: 11555

Answers (8)

Nikhil
Nikhil

Reputation: 1

You can use Recursion for this case:

let arr = ["A","B","C","D","E","Y","Z"];
let index = 0;

function find(str){
  if(str[0] === "E")
  { 
    return str[0];
  }
  else {
    str.shift(); 

//removing 1st element from the str array and calling the function again

        index++;
        return find(str);
      }
    }
    
    console.log("Found",find(arr));
    console.log("at Index:",index);
    console.log("Peace Out! XD");

Upvotes: 0

craftsmannadeem
craftsmannadeem

Reputation: 2933

It is basically a interview question, to test your recursion skills, here is how you do it.

private static <T> int findIndex(T[] items, T item, int index) {
    if (items.length == index) {
        return -1;
    }
    if (items[index].equals(item)) {
        return index;
    }
    return findIndex(items, item, index+1);
}

Here is how you can run it

int val = findIndex(new String[]{"A","B","C"}, "C", 0);

Upvotes: 2

Dino
Dino

Reputation: 561

Here is the code I am working on. This is what I have at the moment. It is just a practice round for me to practice JAVA and understand the Ceaser Cipher.

public class CeaserCipher
{
public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter test letters for Caesar cipher in capitals");
    String input = keyboard.nextLine();
    char[] strArray = input.toCharArray();

    System.out.print("What is the key: "); 
    int key = keyboard.nextInt();
    //String[] arrKey  = new String[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    String c  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] arrKey = c.toCharArray();

    for (int i = 0; i < strArray.length ; i++){

        char cipherValue = strArray[i];
        int index = Arrays.binarySearch(arrKey, cipherValue);
        int j = (key + index)%26;
        System.out.print(arrKey[j]);

    }
}
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533442

It only possible if you assume you have this array (or one similar) as you can calculate the index.

String s = "E";
int index = s.charAt(0) - 'E'; // == 4

Upvotes: 0

Ahmad
Ahmad

Reputation: 12707

Use indexOf

   return arrKey.get(arrKey.indexOf("E"));

Upvotes: 0

mariosangiorgio
mariosangiorgio

Reputation: 5543

If you have to deal with chars and not strings have a look at the getNumericValue method.

If you want a more general solution you should consider using a Map<String,Integer> rather than an array.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

I don't want to do this in a loop.

There has to be a loop somewhere - either in your code or library code.

So yes, you can use

int index = Arrays.asList(arrKey).indexOf("E");

... but that will loop under the covers.

If you know that your array is sorted to start with, you can use:

int index = Arrays.binarySearch(arrKey, "E");

That will be more efficient - but it's still a loop...

Of course, if you know that your array is always A-Z, then you can do it without a loop - but I assume your real case is more generalized...

Upvotes: 16

Related Questions