Write a program to take a String as input then display the position of a particular character given as input using indexOf() function of String class. Don't use charAt() function.
Enter a String
Elephants are the largest mammals
Enter character to find: e
Index Position: 0 2 12 16 22
Don't use ArrayList and Character class, use String class functions if necessary
import java.util.*;
class App {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a String: ");
String string = in.nextLine().toLowerCase();
System.out.print("Enter character to find: ");
String characterFind = in.nextLine().toLowerCase();
int index = string.indexOf(characterFind);
System.out.print("Index Position: ");
while (index >= 0) {
System.out.print(index + " ");
index = string.indexOf(characterFind, index + 1);
}
in.close();
}
}
Comments
Leave a comment