Monday, January 20, 2020

String Programs in Java

  • 1. Count Unique Characters in String in Java.

  • import java.util.ArrayList;
  • import java.util.List;
  • import java.util.Scanner;
  • 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) {
  • String s2 = s1.toLowerCase();
  • StringBuffer sb = new StringBuffer(s2);
  • int l = sb.length();
  • int count = 0;
  • for (int i = 0; i < l; i++) {
  • count = 0;
  • for (int j = i + 1; j < l; j++) {
  • if (sb.charAt(i) == sb.charAt(j)) {
  • sb.deleteCharAt(j);
  • count++;
  • j--;
  • l--;
  • }
  • }
  • if (count > 0) {
  • sb.deleteCharAt(i);
  • i--;
  • l--;
  • }
  • }
  • if (sb.length() == 0) {
  • System.out.println(-1);
  • } else
  • System.out.println(sb.length());
  • }
  • }

2. Java program to reverse a string

import java.util.*;
class ReverseString
{
  public static void main(String args[])
  {
    String original, reverse = "";
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a string to reverse");
    original = in.nextLine();
    int length = original.length();
    for (int i = length - 1 ; i >= 0 ; i--)
      reverse = reverse + original.charAt(i);
    System.out.println("Reverse of the string: " + reverse);
  }
}

Reverse a string in Java using StringBuffer class

class InvertString
{
  public static void main(String args[])
  {
     StringBuffer a = new StringBuffer("Java programming is fun");
     System.out.println(a.reverse());
  }
}



3. Java program asks the user to provide a string input, and checks it for the Palindrome String.

import java.util.Scanner; class ChkPalindrome { public static void main(String args[]) { String str, rev = ""; Scanner sc = new Scanner(System.in); System.out.println("Enter a string:"); str = sc.nextLine(); int length = str.length(); for ( int i = length - 1; i >= 0; i-- ) rev = rev + str.charAt(i); if (str.equals(rev)) System.out.println(str+" is a palindrome"); else System.out.println(str+" is not a palindrome"); } }


4. Java program to remove a particular character from a string.
class GFG 
{
static void removeChar(String s, char c)
{
    int j, count = 0, n = s.length();
    char []t = s.toCharArray();
    for (int i = j = 0; i < n; i++)
    {
        if (t[i] != c)
        t[j++] = t[i];
        else
            count++;
    }
      
    while(count > 0)
    {
        t[j++] = '\0';
        count--;
    }
      
    System.out.println(t);
}
  
// Driver Code
public static void main(String[] args) 
{
    String s = "geeksforgeeks";
    removeChar(s, 'g');
}
}


5. Write a code to prove that strings are immutable in java?


  1. public class ProveStringImmutable {  
  2.     public static void referenceCheck(Object x, Object y) {  
  3.         if (x == y) {  
  4.             System.out.println("Both pointing to the same reference");  
  5.    
  6.         } else {  
  7.             System.out.println("Both are pointing to different reference");  
  8.         }  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         String st1 = "Java";  
  12.         String st2 = "Java";  
  13.         System.out.println("Before Modification in st1");  
  14.         referenceCheck(st1, st2);  
  15.         st1 += "ava";  
  16.         System.out.println("After Modification");  
  17.         referenceCheck(st1, st2);  
  18.     }  
  19. }  


6.Write a Java program to count the number of words in a string?

  1. public class WordCount {  
  2.       static int wordcount(String string)  
  3.         {  
  4.           int count=0;  
  5.       
  6.             char ch[]= new char[string.length()];     
  7.             for(int i=0;i<string.length();i++)  
  8.             {  
  9.                 ch[i]= string.charAt(i);  
  10.                 if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )  
  11.                     count++;  
  12.             }  
  13.             return count;  
  14.         }  
  15.       public static void main(String[] args) {  
  16.           String string ="    India Is My Country";  
  17.          System.out.println(wordcount(string) + " words.");   
  18.     }  
  19. }  

7. Write a Java program to check whether two strings are anagram or not?

  1. import java.util.Arrays;  
  2.    
  3. public class AnagramString {  
  4.     static void isAnagram(String str1, String str2) {  
  5.         String s1 = str1.replaceAll("\\s""");  
  6.         String s2 = str2.replaceAll("\\s""");  
  7.         boolean status = true;  
  8.         if (s1.length() != s2.length()) {  
  9.             status = false;  
  10.         } else {  
  11.             char[] ArrayS1 = s1.toLowerCase().toCharArray();  
  12.             char[] ArrayS2 = s2.toLowerCase().toCharArray();  
  13.             Arrays.sort(ArrayS1);  
  14.             Arrays.sort(ArrayS2);  
  15.             status = Arrays.equals(ArrayS1, ArrayS2);  
  16.         }  
  17.         if (status) {  
  18.             System.out.println(s1 + " and " + s2 + are anagrams");  
  19.         } else {  
  20.             System.out.println(s1 + " and " + s2 + are not anagrams");  
  21.         }  
  22.     }  
  23.    
  24.     public static void main(String[] args) {  
  25.         isAnagram("Keep""Peek");  
  26.         isAnagram("Mother In Law""Hitler Woman");  
  27.     }  
  28. }  

No comments:

Post a Comment

String

  Java String  class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), re...