prowessapps.in String Handling Programs

 WAP to display the length of a string.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String name=s.nextLine();
  int len = name.length();
  System.out.print("Length = "+len);
 }
}
OUTPUT :
Enter String: Java Prowesss
Length = 12

 WAP to copy one string to another.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String s1=s.nextLine();
  String s2=s1;
  System.out.print("Copied String: "+s2);
 }
}
OUTPUT :
Enter String: Java Prowess
Copied String: Java Prowess

 WAP to concatenate two string.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter 1st String: ");
  String s1=s.nextLine();
  System.out.print("Enter 2nd String: ");
  String s2=s.nextLine();
  String s3=s1.concat(s2);
  System.out.print("Concat. String: "+s3);
 }
} 
OUTPUT :
Enter 1st String: Java
Enter 2nd String: Prowess
Concat. String: JavaProwess

 WAP to count total vowels in a String.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String s1 = s.nextLine();
  char x;
  int count=0;
  for(int i=0; i< s1.length(); i++) 
  {
   x = s1.charAt(i);
   if(x=='A' ||x=='E'||x=='I'||x=='O'
   ||x=='U'||x=='a'||x=='e'||x=='i'
   ||x=='o'||x=='u') {
    count++;
   }
  }
  System.out.print("Vowels = "+count);
 }
}  
OUTPUT :
Enter String: Java Prowess
Vowels = 4

 WAP to compare two string.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter 1st String: ");
  String s1 = s.nextLine();
  System.out.print("Enter 2nd String: ");
  String s2 = s.nextLine();
  if(s1.equals(s2)) {
   System.out.print("SAME STRING");
  }
  else {
   System.out.print("DIFFERENT STRING");
  }
 }
}
OUTPUT :
Enter 1st String: java prowess
Enter 2nd String: c++ prowess
DIFFERENT STRING

 WAP to display reverse of a String.

import java.util.Scanner;
class Test {
 public String reverse(String x) {
  String rev = "";
  for(int i=x.length()-1; i>=0;i--) {
   rev = rev + x.charAt(i);
  }
  return rev;
 }
 public static void main(String[] a)
 {
  Test t = new Test();
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String s1 = s.nextLine();
  String s2 = t.reverse(s1);
  System.out.print("Reverse = "+s2);
 }
}
OUTPUT :
Enter String: Java prowess
Reverse = sseworp avaJ 


 WAP to check a given string is palindrome or not.

import java.util.Scanner;
class Test {
 public String reverse(String x) {
  String rev = "";
  for(int i=x.length()-1; i>=0;i--) {
   rev = rev + x.charAt(i);
  }
  return rev;
 }
 public static void main(String[] a)
 {
  Test t = new Test();
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String s1 = s.nextLine();
  String s2 = t.reverse(s1);
  if(s1.equals(s2)) {
   System.out.print("Palindrome");
  }
  else {
   System.out.print("Not Palindrome");
  }
 }
}
OUTPUT :
Enter String: MADAM
Palindrome

 WAP to convert lower case to upper case.

import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String s1 = s.nextLine();
  String s2 = s1.toUpperCase();
  System.out.print("Upper Case = "+s2);
 }
}
OUTPUT :
Enter String: java Prowess
Upper Case = JAVA PROWESS

 WAP to reverse a string "WORD BY WORD".

import java.util.Scanner;
class Test {
 public String reverse(String x) {
  String rev = "";
  for(int i=x.length()-1; i>=0;i--) {
   rev = rev + x.charAt(i);
  }
  return rev;
 }
 public static void main(String[] a)
 {
  Test t = new Test();
  Scanner s=new Scanner(System.in);
  System.out.print("Enter String: ");
  String s1 = s.nextLine();
  String[] x = s1.split(" ");
  String s2="";
  for(int i=0; i< x.length; i++) {
   s2 =s2+ t.reverse(x[i])+" ";
  }
  System.out.print("Reverse = "+s2);
 }
}
OUTPUT :
Enter String: Java Prowess App
Reverse = avaJ sseworP ppA

 Create a String array that stores name of students and then perform namewise sorting on that array to sort the list of students.

lass Test {
 void sort(String[] x) {
  int n = x.length;
  String temp;
  for(int i = 0; i < n; i++) {
   for(int j = i + 1; j < n; j++) {
    if(x[i].compareTo(x[j])>0) {
     temp = x[i];
     x[i] = x[j];
     x[j] = temp;
    }
   }
  }
  System.out.println("Sorted Order:");
  for (int i = 0; i < n-1; i++) {
   System.out.print(x[i] + "   ");
  }
 }
 public static void main(String[] a)
 {
  String[] x = {"Daneyal","Arif", "Faiz","Ayan","Alok"};
  Test t = new Test();
  t.sort(x);
 }
}
OUTPUT :
Sorted Order:
Alok   Arif   Ayan   Daneyal   Faiz




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved