Showing posts with label String Manipulation Programs. Show all posts
Showing posts with label String Manipulation Programs. Show all posts

Monday, 9 May 2016

JAVA program for printing the Capital letters in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to print the Capital letters in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

L U C C L U C J A V A

Program:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any Sentence");
String ss = s.nextLine();
String[ ] a = ss.split(" ");
String res = "";
for(int i = 0;i<a.length;i++)
{
String str = a[i];
for(int j = 0;j<str.length();j++)
{
int ch = str.charAt(j);
if(ch<=90 && ch>=65)
{
res+ = str.charAt(j)+" ";
}
}
}
System.out.println(res);
}
}


Source Code: download

Sunday, 8 May 2016

JAVA program for reversing the words in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to reverse the each word in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

teL sU edoC ni C teL sU edoC ni AVAJ

Program:

import java.util.Scanner;
public class Test{
           public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter any Sentence:");
      String ss = s.nextLine( );
      String[ ] a = ss.split(" ");
      String res="";
      for(int i=0;i<a.length;i++){
         StringBuffer s1 = new StringBuffer(a[i]);
         s1.reverse( );
         res+=s1+" ";
      }
      System.out.println(res);
    }
}


Source Code: download


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