Write a java statement for each to perform the following task: (i) Find and display the position of the last space in a string str
help kar do mere pyaare mates❤❤
Answers
Explanation:
class Index
{
public static void main (String args [])
{
String str="kunal_ Kashyap";
System.out.println(str.lastIndexOf(" ")+1);
}
}
So,the java statement is:
System.out.println(str.lastIndexOf(" ")+1);
Working:
*the function lastIndexOf() returns the index of last position of space in the string.but,we need to add 1 to it as index starts from 0.
Program:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String str="Lead a happy life, pal";
System.out.println("The position of the last space in the string str is: "+(str.lastIndexOf(" ")+1));
System.out.println("Second character of the string str is: "+ str.charAt(1));
}
}
Output:
The position of the last space in the string str is: 19
Second character of the string str is: e
Learn more:
1. Write a program to check whether it is a Lead number or not in java
https://brainly.in/question/15644815
2. Write a program in Java to generate the following pattern using nested loops.
brainly.in/question/18819254