Java Questions :Simple String Manipulation



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.
Sample Input 1:
hello
Sample Output 1:
llo
Sample Input 2:
java
Sample Output 2:
jva


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);
}

}
SHARE
    Blogger Comment
    Facebook Comment

3 comments :

  1. Great post for Java String .
    thanks for this useful post.

    ReplyDelete
  2. import java.util.*;

    public class UserMainCode
    {
    public static String getString(String s)
    {
    char []charr = s.toCharArray();
    int l = charr.length;
    char []carr=new char[l];
    System.out.println(l);
    int i=0,j=0;
    if((s.charAt(0)=='j') && (s.charAt(1)=='b'))
    {
    return s;
    }
    else if((s.charAt(0)=='j') && (s.charAt(1)!='b'))
    {
    for(i=0;i<l-1;i++)
    {
    if(i==1)
    {
    carr[i]=charr[j+1];
    j++;
    }
    else
    carr[i]=charr[j];
    j++;

    }
    }
    else if((s.charAt(0)!='j') && (s.charAt(1)=='b'))
    {
    for(i=0;i<l-1;i++)
    {
    carr[i]=charr[j+1];
    j++;
    }
    }
    else
    {
    //if((s.charAt(0)!='j') && (s.charAt(1)!='b'))
    //{
    j=2;
    for(i=0;i<l-2;i++)
    {
    carr[i]=charr[j];
    j++;
    }
    //}
    }
    String nstr = String.copyValueOf(carr);
    return nstr;
    }
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    System.out.println(UserMainCode.getString(str));
    }
    }

    It works fine!!!

    ReplyDelete
  3. import java.util.Scanner;
    public class Main{

    public static void main(String s[]) {
    Scanner sc= new Scanner(System.in);
    System.out.println(UserMainCode.getString(sc.nextLine()));
    }
    }

    class UserMainCode {
    public static String getString(String str) {
    if(str.charAt(0)!='j' && str.charAt(1)!='b' ) {
    str=str.substring(2);
    }
    else if(str.charAt(0)=='j' && str.charAt(1)!='b' ) {
    str=str.substring(0,1)+str.substring(2);
    }
    else if(str.charAt(0)!='j' && str.charAt(1)=='b' ) {
    str=str.substring(1);
    }
    return str;
    }
    }

    ReplyDelete

loading...