21 to 30 String Programs

100 Java String Programming Questions

100 Java String Programming Questions

21 to 30 Java Programs

21//Write a program to compare two strings lexicographically.

        class lexicographically {
        public static void main(String[] args) {
        String str1="apple";
        String str2="Apple";
        int comparedValue=(str1.compareTo(str2));
        if(comparedValue>0){
            
            System.out.println(str1+ "  will comes after  "+str2);
        }
        else if(comparedValue<0){
            
            System.out.println(str2+ "  will comes after  "+str1);
        }
        else{
            System.out.println("Both are same string");
        }
    }
}

22//Write a program to concatenate two strings.

    public class StringConcatenation {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "World";
        
       
        String result1 = str1 + " " + str2;
        System.out.println("Concatenated string: " + result1);
        
         String result2 = str1.concat(" ").concat(str2);
        System.out.println("Concatenated using concat: " + result2);
        
        String result3 = String.join(" ",str1,str2);
        System.out.println("Concatenated using join(,,): " + result3);
        
        StringBuilder sb=new StringBuilder();
        sb.append(str1);
        sb.append(" ");
        sb.append(str2);
        System.out.println("Concatenated using stringbuilder: " + sb);
        
        String result5=String.format("%s  %s",str1,str2);
         System.out.println("Concatenated using format: " + result5);
        
        
    }
}

23//Write a program to check if a string contains a given substring. .

    public class SubstringCheckTwoPointer {
    public static boolean isSubstringTwoPointer(String main, String sub) {
        int m = main.length();
        int n = sub.length();

        if (n == 0) return true;

        int i = 0, j = 0;
        while (i < m) {
            if (main.charAt(i)==sub.charAt(j)) {
                j++;
                if(j==n) return true; 
            } else{
                if(j > 0) {
                    i = i-j; }
                j=0;      }
            i++;
        }
        return false;
    }

    public static void main(String[] args) {
        String stringmain = "Justin Livingston";
        String given = "st";
        boolean result = isSubstringTwoPointer(stringmain, given);
        System.out.println("Two Pointer check: " + result);
    }
}
//str1.contains(str2);
//str1.indexOf(str2);

24//Write a program to find the index of the first occurrence of a character. .

24//Write a program to find the index of the first occurrence of a character.
public class FirstOccurrence {
    public static int findFirstOccurrence(String str, char target) {
        
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == target) {
               return i;
            }
        }
        return -1; 
    }

    public static void main(String[] args) {
        String text = "Programming in Java";
        char search = 'a';

        int index = findFirstOccurrence(text, search);

        if (index != -1) {
            System.out.println("First occurrence of '" + search + "' is at index: " + index);
        } else {
            System.out.println("Character '" + search + "' not found in the string.");
        }
    }
}
//str1.indexOf(char);
//str1.lastIndexOf(char);

25//Write a program to . .





  

Comments

Popular posts from this blog

Strings

Arrays