Java Questions :Simple String Manipulation

$ads={1}

Java Questions :Simple String Manipulation

 

$ads={1}

Write a program to read a string and return a modified string based on the following rules.

Return the String without the first 2 chars except when
1.   keep the first char if it is 'j'
2.   keep the second char if it is 'b'.
Include a class UserMainCode with a static method getString which accepts a string. The return type (string) should be the modified string based on the above rules. Consider all letters in the input to be small case.
Create a Class Main which would be used to accept Input string and call the static method present in UserMainCode.
Input and Output Format:
Input consists of a string with maximum size of 100 characters.
Output consists of a string.
Refer sample output for formatting specifications.

$ads={1}

Sample Input 1:
hello
Sample Output 1:
llo
Sample Input 2:
java
Sample Output 2:
jva


$ads={1}

import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();

StringBuffer sb=new StringBuffer();
if(s.charAt(0)=='j')
{
if(s.charAt(1)=='b')
{
sb.append(s);
}
else
{
sb.append(s.charAt(0)).append(s.substring(2));
}
}
else if(s.charAt(1)=='b')
{
sb.append(s.charAt(0)).append(s.substring(2));
}
else
{
sb.append(s.substring(2));
}
System.out.println(sb);
}

}

$ads={1}

Post a Comment

Post a Comment (0)

Previous Post Next Post