Question

WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search,...

WRITTEN IN JAVA

Complete the method printIndices that accepts a String source and a character search, and prints the position of each occurrence of the search value in source. For example:

Input: source = "String Programming in Java is fun", search = 'i'

Console Output: 3 15 19 27

Template provided from question:

   public static int printIndices(String source, char search)
   {


   }

   public static void main(String[] args)
   {

       System.out.println("Expected printIndices() prints indices 3 15 19 27 for search letter i : ");
       printIndices(sentence, 'i');
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class PrintIndices {

    public static void printIndices(String source, char search)
    {
        for (int i = 0; i < source.length(); i++) {
            if (source.charAt(i) == search) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        System.out.println("Expected printIndices() prints indices 3 15 19 27 for search letter i : ");
        printIndices("String Programming in Java is fun", 'i');
    }
}
Add a comment
Know the answer?
Add Answer to:
WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • Write a JAVA program that has a method which accepts a string and prints it back...

    Write a JAVA program that has a method which accepts a string and prints it back to the user with all vowels replaced with *'s (multiple asterisks not '*s') with a new line after the string. The method signature should look like this: public static void replacePrint(String input)

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • Need the answer in Java 1.17 LAB*: Program: ASCII art This zyLab activity is the traditional...

    Need the answer in Java 1.17 LAB*: Program: ASCII art This zyLab activity is the traditional programming assignment, typically requiring a few hours over a week. The previous sections provide warm up exercises intended to help a student prepare for this programming assignment. (1) Output this tree. (2 pts) * *** ***** ******* *** (2) Below the tree (with two blank lines), output this cat. (3 pts) /\ /\ o o = = --- Hint: A backslash \ in a...

  • Java Method Resolution : Consider the following example: What is printed on the console if Main...

    Java Method Resolution : Consider the following example: What is printed on the console if Main is executed, and why? How the codes could be improved? public class Main {     public static void main(String[] args) throws Exception {         A obj = null;         obj.foo();     } } public class A {     public void foo() {         System.out.println(“foo in A”);     } }

  • Java 7 Write a constructor for the MightyByte class so it can accept a String argument...

    Java 7 Write a constructor for the MightyByte class so it can accept a String argument and set the bits field. Complete the for loop in the getDecimalValue method. class Main { public static void main(String[] args) { MightyByte myByte = new MightyByte("00000101"); System.out.println(myByte.getDecimalValue());    } } class MightyByte { private String bits;    // write the constructor here    public int getDecimalValue() { int decimalValue = 0; int intValue; int power = 7; for (int i = 0; i...

  • Write a static method called encodeString that takes a string as input and prints to the...

    Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...

  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • Enhanced Binary Search Consider the binary search algorithm in Section 14.6. If no match is found,...

    Enhanced Binary Search Consider the binary search algorithm in Section 14.6. If no match is found, the search method returns -1. Modify the method so that if a is not found, the method returns -k - 1, where k is the position before which the element should be inserted. (This is the same behavior as Arrays.binarySearch.) Upload a file named BinarySearcher.java that has the same methods the binary search class in Section 14.6. Test Case 2 Test Case 1 Console...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT