Question

In: Computer Science

Write a Java method that takes an array of char and a String as input parameters...

Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.

Solutions

Expert Solution

Solution :

Steps for the program :

  • To check if the given string is present in char array either forwards or backwards. We can individually check for the given string and its reverse string, if they any one of them is present in the char array, return true else return false.
  • To check if a given string is present in char array, we can simply use indexOf() function, it returns the index of the where the given string is present in the char array.
  • If string is not present in the char array, it returns -1
  • To test the program, I have used 3 cases : case when string is present forwards, case in which string is present backwards, case in which string is not present at all.

Following is the Java program for the same :

public class solve {
    public static boolean isPresent(char[] charArray, String s)
    {   //This string will store reverse of string s 
        String rev="";  
        for(int i=s.length()-1;i>=0;i--){  
        rev+=s.charAt(i);  
    }  
        // Check for the condition for forward string and backward string
        if (new String(charArray).indexOf(s) == -1 &&  new String(charArray).indexOf(rev) == -1)
            return false;   //string is not present
        else return true;   //string is present
    }
    public static void main(String args[]) {
      //Testing
      
      //Test 1
      String s1="Programmer";
      char []array1={'c','d','P','r' , 'o' , 'g' , 'r' , 'a' , 'm' , 'm' , 'e' , 'r' , 'e' , 'H' , 'A' , 'S'};
      if(isPresent(array1,s1))
      System.out.println("Given string is present in the char array!");
      else System.out.println("Given string is not present in the char array (forwards and backwards)!");
      
      //Test 2
      String s2="Hello World!";
      char []array2={'c','d','!','d' , 'l' , 'r' , 'o' , 'W' , ' ' , 'o' , 'l' , 'l' , 'e' , 'H' , 'A' , 'S'};
      if(isPresent(array2,s2))
      System.out.println("Given string is present in the char array!");
      else System.out.println("Given string is not present in the char array (forwards and backwards)!");
      
      //Test 3
      String s3="Thank You";
      char []array3={'f','u','e','r' , '-' , 'r' , 'h' , 'T' , ' ' , 'o' , 'l' , 'l' , 'e' , 'Y' , 'O' , 'U'};
      if(isPresent(array3,s3))
      System.out.println("Given string is present in the char array!");
      else System.out.println("Given string is not present in the char array (both forwards and backwards)!");
    }
}

Code demo :

Output :


Related Solutions

Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT