Computer Science, asked by vritu1470, 10 months ago

program to count the position of first space and last space in java​

Answers

Answered by TheArkhamKnight
2

Answer:

import java.util.Scanner;  // Import the Scanner class

class Main {

 public static void main(String[] args) {

   Scanner input = new Scanner(System.in);  // Create a Scanner object

System.out.println("Enter a string: ");  //Ask user for input

String enteredString = input.nextLine();  //Get input with scanner object

int firstInstance = enteredString.indexOf(" ");  //Find the first instance of space

int lastInstance = enteredString.lastIndexOf(" ");  //Find the last instance of space

System.out.println("First instance of space was at " + firstInstance + ". Last instance of space was at " + lastInstance + ".");   //Print them both

 }

}

Explanation:

Similar questions