Question

In: Computer Science

JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { }...

JAVA coding language:

Consider the method getAllCharAsString:

public statis String getAllCharAsString(String inputStr, char target) {

}

The method accepts a String parameter, inputStr, and a char parameter, target. It returns the result string consisting of all the characters in inputStr which match the target character (matching is case sensitive, for example, 'c' does not match "C"). If the characters match, the character is appended to the result String. After all the characters in inputStr have been compared, the method returns the result String. The method returns the empty String if there were no matches. Assume the inputStr is a String with length >= 0.

Example 1:

String str = "Open the pod bay doors Hal.";

String resStr = getAllCharsAsString(str, 'o');

resStr would be the String, "ooo".

Example 2:

String str = "Open the pod bay doors Hal.";

String resStr = getAllCharsAsString(str, 'z');

resStr would be the empty String, " ".

Write the getAllCharAsString method body in the box below.

Solutions

Expert Solution

CODE:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
       Scanner scan = new Scanner(System.in);
       //taking inputs from user.
       System.out.print("Enter string : ");
       String inputStr = scan.nextLine();
       System.out.print("Enter Character : ");
       char target = scan.next().charAt(0);
       //calling function to get result string.
       String resStr = getAllCharAsString(inputStr,target);
       System.out.println("The result string is : "+resStr);
      
   }
   public static String getAllCharAsString(String inputStr, char target)
   {
       String result ="";//Result string
       //converting target to string
       String tar = Character.toString(target);
       //loop through the string.
       for (int i=0;i<inputStr.length();i++)
       {
           //condition to check the target in string.
           if(inputStr.charAt(i) == target)
           {
               result += tar;//adding the chars matched to string.
           }
       }
       return result;//returning the resultant string.

   }

}

CODE ATTACHMENTS:

OUTPUT:

Please do comment for any queries.
Please like it.
Thank you.


Related Solutions

How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it...
How to write Method in Java: public static in firstLetterFreq(char letter, String[] words] { // it returns 0 if (words == null OR words.length == 0) // returns number of times letter is the first letter of a String in words array // use equalIgnoreCase to check if letter and str.charAt(0) are equal and ignoring case }
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.
Programming language is Java. Consider you have already created a validateString(String str) method that validates a...
Programming language is Java. Consider you have already created a validateString(String str) method that validates a String to ensure it contains exactly 4 characters. Create code that calls this method, sending user input as an argument (actual parameter). The user should be continuously prompted to enter a different string and informed of an error if the method returns false.
Java- creat a method that takes two char parameters. Return a String containing all characters, in...
Java- creat a method that takes two char parameters. Return a String containing all characters, in order, from the first char parameter (inclusive) to the last (inclusive). For instance, input('a', 'e'), and return "abcde".
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT