Sunday 3 April 2016

Write a program which accepts 2 strings from the user and performs string concatenation in JAVA



Write a program which accepts 2 strings from the user and performs the following operation on it.

Example1:

String1 = "JAVA"
String2 = "PYTHON"
String3 must be "JAVANOHTYP"

Example2:

String1 = "Vignesh"
String2 = "Sundeep"
String3 must be "VigneshpeednuS"

Both strings must be concatenated but after reversing the second string (as shown in string3 above).

Program:

import java.util.Scanner;

public class String_Program {
public static void main(String[ ] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any two strings: ");
String s1 = s.next();
String s2 = s.next();
StringBuffer sb = new StringBuffer(s2);
sb.reverse( );
System.out.println(s1+sb);
}
}

Input/Output:

Enter any two strings:
Raju
Ramana
RajuanamaR


String Manipulation Program-1 in JAVA


Description:

10 digit number: 1234567812
10 digit number in words: ONETWOTHREEFOURFIVESIXSEVENEIGHTONETWO

Constraints:

1) remove duplicate letters in the above string.
2) final string must be in sorted order.

Final String: EFGHINORSTUVWX

Program:

import java.util.Arrays;
import java.util.Scanner;

public class String_Program{
public static boolean ISin(String str, char c) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter 10 digit number: ");
long n = s.nextLong();
String str = n + "";
String res = "";
String no[] = new String[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" };
if (str.length( ) < 10 || str.length( ) > 10) {
System.out.println("Invalid Input");
} else {
for (int i = 0; i < str.length(); i++) {
res = res + no[Integer.parseInt(str.charAt(i) + "")];
}
}
String temp = "";
for (int i = 0; i < res.length(); i++) {
if (!ISin(temp, res.charAt(i))) {
temp = temp + res.charAt(i);
}
}
String r[] = new String[temp.length()];
for (int i = 0; i < r.length; i++) {
r[i] = temp.charAt(i) + "";
}
String rr = "";
Arrays.sort(r);
for (int i = 0; i < r.length; i++) {
rr += r[i];
}
System.out.println(rr);

}
}

Input/Output:

Enter 10 digit number:
1234567812
EFGHINORSTUVWX



Sum of last digits of two given numbers program in JAVA



Program:

import java.util.Scanner;
public class Sum_of_last_digits_of_two_given_numbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any two numbers: ");
int a = s.nextInt();
int b = s.nextInt();
String s1 = a+""; //converting integer to string
String s2 = b+""; //converting integer to string
int c = Integer.parseInt(s1.charAt(s1.length()-1)+"") + Integer.parseInt(s2.charAt(s2.length()-1)+"");
System.out.println("Sum of last digits of two given numbers is: "+c);
}
}

Input/Output:

Enter any two numbers:
123
145
Sum of last digits of two given numbers is: 8