Write a java program to String Processing

Write a program to read two strings and one int value(N). check if the Nth character of first String from the start and Nth character of second String from the end are same or not. If both are same return true else return false.
Check need not be Case sensitive

Input and Output Format:
Input consists of two strings and an integer.
The output consists of TRUE / FALSE.
Refer sample output for formatting specifications.
Sample Input 1:
AAAA
abab
2
Sample Output 1:
TRUE
Sample Input 2:
MNOP
QRST
3

String Processing in Java.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    String s2 = sc.nextLine();
    int n = sc.nextInt();
    boolean b = lengthiestString(s1, s2, n);
    if (b == true) {
      System.out.print("TRUE");
    } else {
      System.out.print("FALSE");
    }
  }

  public static boolean lengthiestString(String s1, String s2, int n) {
    boolean a = false;
    char c = s1.charAt(n);
    char d = s2.charAt(s2.length() - n);

    String s3 = Character.toString(c);
    // System.out.println(s3);
    String s4 = Character.toString(d);
    // System.out.println(s4);
    if (s3.equalsIgnoreCase(s4)) {
      a = true;
    } else {
      a = false;
    }
    return a;
  }
}

Write a java program to String Processing – II.

Write a program to read a string where all the lowercase ‘x’ chars have been moved to the end of the string.

The return type is the modified string.
Input and Output Format:
Input consists of a string.
The output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1:
xxhixx

Sample Output 1:
hixxxx

Sample Input 2:
XXxxtest

Sample Output 2:
XXtestxx

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    String s1 = s.replaceAll("[x]", "");
    String s2 = s.replaceAll("[^x]", "");
    
    System.out.println(s1 + s2);
  }
}

Write a java program to String Processing – III.

Write a program to read a string and also a number N. Form a new string starting with the 1st character and with every Nth character of the given string. Ex – if N is 3, use chars 1, 3, 6, … and so on to form the new String. Assume N>=1.

The return type is the string as per the problem statement.

Input and Output Format:

Input consists of a string and integer.
The output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1:
HelloWorld
2

Sample Output 1:
HelWrd

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
int n=sc.nextInt();


StringBuffer sb=new StringBuffer();
String s2=s1.replaceAll(" ","");

sb.append(s2.charAt(0));
//System.out.println(sb);
for(int i=n-1;i<s2.length();i=i+n)
{
  sb.append(s2.charAt(i));
}
System.out.println(sb);
}
}

Write a java program to String Processing – IV.(String Processing – MixMania)

Write a program to read a string and check if it starts with ‘_ix’ where ‘_’ is any one char(a-z, A-Z, 0-9).
If specified pattern is found return true else false. The return type is TRUE / FALSE.
Input and Output Format:
Input consists of a string.
Output consists of TRUE / FALSE.
Refer sample output for formatting specifications.

Sample Input 1:
Mix Mania

Sample Output 1:
TRUE

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    StringBuffer sb = new StringBuffer();
    String s2 = s1.substring(0, 3);

    int a = 0, b = 0, c = 0;
    char c21 = s2.charAt(0);
    if (Character.isDigit(c21) || Character.isLetter(c21)) {
      a = 1;
    }

    if (a == 1) {
      char c1 = s2.charAt(1);
      char c2 = s2.charAt(2);
      if (c1 == 'i') {
        b = 1;
      }
      if (c2 == 'x') {
        c = 1;
      }

    }
    if (a == 1 && b == 1 && c == 1) {
      System.out.print("true");
    } else {
      System.out.print("false");
    }

  }
}

Write a java program to String Processing – V.

Write a program to read a string and return a new string where the first and last chars have been interchanged.
The return type is the modified string.

Input and Output Format:

Input consists of a string.
The output consists of string.
Refer sample output for formatting specifications.

Sample Input 1:
HelloWorld

Sample Output 1:
delloWorlH

import java.util.Scanner;

public class Main {

  /**
   * @param args
   */
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    String s1 = sc.nextLine();
    StringBuffer sb = new StringBuffer();
    char c = s1.charAt(0);
    String s2 = s1.substring(1, s1.length() - 1);
    char c1 = s1.charAt(s1.length() - 1);
    sb.append(c1).append(s2).append(c);
    System.out.println(sb);
  }

}

Write a java program to String Processing – VI(Strings Processing – Replication).

Write a program to read a string and also a number N. Return the replica of original string for n given time.

The return type is the string based on the problem statement.

Input and Output Format:

Input consists of a string and integer.
The output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1:
Lily
2

Sample Output 1:
LilyLily

import java.util.Scanner;

public class Main{
  public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    String s=sc.next();
    int n=sc.nextInt();
    String s2=validString(s,n);
    System.out.println(s2);
  }
  public static String validString(String s,int n){
    
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<n;i++)
    {
      sb.append(s);
    }
    
    return sb.toString();
  
}
}

Write a java program to String Processing – VII.

Write a program to read a string array, concatenate the array elements one by one separated by the comma and return the final string as output.

Input and Output Format:
Input consists of an integer n which is the number of elements followed by n string values.
The output consists of the string.
Refer sample output for formatting specifications.

Sample Input 1:
3
AAA
BBB
CCC
Sample Output 1:
AAA,BBB,CCC

import java.util.*;
 
public class Main {
 
                public static void main(String[] args)  {
                               Scanner sc = new Scanner(System.in);
                               int n = sc.nextInt();
                               String s1[] = new String[n];
                               for(int i=0;i<n;i++){
                            	   s1[i] = sc.next();
                               }
                                
                                StringBuffer sb=new StringBuffer();
                                for(int i=0;i<s1.length;i++)
                                {
                                                sb.append(s1[i]).append(",");
                                               
                                }
                                sb.deleteCharAt(sb.length()-1);
                                System.out.println(sb);
                }
}

Write a java program to String Processing – VIII(String Processing – Username).

Write a program to read a valid email id and extract the username.
The return type is the modified string.
Note – username is the string appearing before @ symbol.

Input and Output Format:
Input consists of a string.
Output consists of string.
Refer sample output for formatting specifications.

Sample Input 1:
admin@xyz.com

Sample Output 1:
admin

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    getvalues(s1);
  }

  public static void getvalues(String s1) {

    StringTokenizer st = new StringTokenizer(s1, "@");
    String s2 = st.nextToken();
    System.out.println(s2);

  }
}

Write a java program to String Processing – XI.

Write a program to read a string and return a new string which is made of every alternate character starting with the first character. For example, NewYork will generate Nwok, and Samurai will generate Smri.

Input and Output Format:
Input consists of a string.
The output consists of string.
Refer sample output for formatting specifications.

Sample Input 1:
Hello

Sample Output 1:
Hlo

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    System.out.println(alternatingChar(s));
  }

  public static String alternatingChar(String s)
                {

                	            String s1=s.replaceAll(" ", "");
                                StringBuffer sbf = new StringBuffer();
                                for(int i = 0; i < s.length() ; i=i+2)
                                {
                                                sbf.append(s.charAt(i));
                                                                                
                                }
                                String str = sbf.toString();
                                return str;
                }
}

Write a java program to String Processing – X.

Write a program to read a string and also a number N. Form a new string made up of n repetitions of the last n characters of the String. You may assume that n is 1 and the length of the string.

The return type is the string as per the problem statement.

Input and Output Format:
Input consists of a string and integer.
Output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1:
Hello
2
Sample Output 1:
lolo

Sample Input 2:
Hello
3
Sample Output 2:
llollollo

import java.util.Scanner;

public class Main
{
                public static void main(String[] args)
                {				Scanner sc = new Scanner(System.in);
                                String s1=sc.nextLine();
                                int n1=sc.nextInt();
                                System.out.println(formattingOfString(s1,n1));
                }
                public static String formattingOfString(String s1, int n1)
                {
                                StringBuffer sb = new StringBuffer();
                                for(int i = 0 ; i < n1 ; i++)
                                                sb.append(s1.substring(s1.length()-n1, s1.length()));
                                return sb.toString();
                }             
}

Add a Comment

Your email address will not be published. Required fields are marked *

1 × two =