Question

In: Computer Science

Exercise 3 – Strings Using a function Write a program that prompts the user to enter...

Exercise 3 – Strings

Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”.

Example1:

Input1: my dog and myself are going to my friend

Input2: my

Output: 0 11 31

Example 2:

Input1: Programming is fun

Input 2: my

Output: not found

Solutions

Expert Solution

import java.util.Scanner;

public class SubStringsIndexes {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1, s2;
        int ind, sum = 0;
        System.out.print("Input 1: ");
        s1 = scanner.nextLine();
        System.out.print("Input 2: ");
        s2 = scanner.nextLine();

        if(s1.contains(s2)) {
            while (s1.contains(s2)) {
                ind = s1.indexOf(s2);
                System.out.print((sum+ind) + " ");
                sum += s2.length()+ind;
                s1 = s1.substring(ind + s2.length());
            }
            System.out.println();
        }
        else{
            System.out.println("not found");
        }
    }
}


Related Solutions

Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Exercise 4 – Lists and input Using a function Write a program that prompts the user...
Exercise 4 – Lists and input Using a function Write a program that prompts the user to input a sequence of words. The program then displays a list of unique words (words that only occurred once, i.e. no repeated).
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
Write a program that prompts the user to enter a 3 x 3 matrix of double...
Write a program that prompts the user to enter a 3 x 3 matrix of double values and tests whether it is a positive Markov matrix. There will be two methods which will be called from the main method: public static double [] [] createArray() 1. Creates a 3 by 3 two dimensional array of doubles 2. Prompts the user for values as shown in the sample run 3. Stores the numbers in the array in the order entered 4....
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they would like to enter a real number. If yes, prompt the User for the real number. Continue to do this until the User enters “no” to the first question. After the User enters “no”, display the average of all the numbers entered. (Using Matlab) and "while" function 2.   Write a program that prompts the User for if they would like to enter a real...
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
Write a Python program using functions and mainline logic which prompts the user to enter a...
Write a Python program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum, the numbers should...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT