IBM
Company
Programming
Arrays
Input:Hello world,l
Output:9
Input :Hello world,m
Output:-1
character 'l' is repeated 3 times we have to return the index value of last 'l'
character 'm' is not there so return -1
Read Solution (Total 7)
-
- Can we use scanner class in program???????
- 7 years agoHelpfull: Yes(5) No(0)
- public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String reverse = " ";
int count = 0;
System.out.println("Enter a string");
String s = sc.nextLine();
for (int i = s.length() - 1; i >= 0; i--) {
reverse = reverse + s.charAt(i);
}
reverse.toString();
System.out.println("enter the character");
char c = sc.next().charAt(0);
for (int i = 0; i < reverse.length(); i++) {
if (c == reverse.charAt(i)) {
if (count == 0) {
int x = s.length() - reverse.indexOf(c);
System.out.println(x);
count++;
}
}}
if (count == 0) {
System.out.println("-1");
}
}
} - 7 years agoHelpfull: Yes(4) No(2)
- hi,who have attended the previous drives plzz post java questions and your interview experience.plzz it will be helpful for us a lot.
- 7 years agoHelpfull: Yes(3) No(0)
- import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
String name=sc.nextLine(); //input of a string
String n=sc.nextLine(); //input of the string you need to find
System.out.println(name.lastIndexOf(n));
}
} - 6 years agoHelpfull: Yes(3) No(2)
- import java.lang.*;
import java.util.Scanner;
class Abc
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String a;
System.out.println("Enter the String");
a=in.nextLine();
System.out.println("Enter the word:");
char b=in.next().charAt(0);
int count=0;
for(int i=a.length()-1;i>=0;i--)
{
char ch=a.charAt(i);
if(ch==b)
{
System.out.println(i);
count++;
break;
}
}
if(count==0)
System.out.println("-1");
}
} - 6 years agoHelpfull: Yes(2) No(0)
- use String class lastIndexOf() method.
eg.
String str = "sanraj todkar";
System.out.println(str.lastIndexOf("a")); - 7 years agoHelpfull: Yes(1) No(0)
- hiii hari please share english materials
- 7 years agoHelpfull: Yes(0) No(0)
IBM Other Question