Java Strings Introduction

Sort by

recency

|

1249 Discussions

|

  • + 2 comments

    What is wrong with the code below? It failed all test cases

    public static void main(String[] args) {
            
            Scanner sc=new Scanner(System.in);
            String A=sc.next();
            String B=sc.next();
            /* Enter your code here. Print output to STDOUT. */
            System.out.println(A.length() + B.length());
            char[] CA = A.toCharArray();
            char[] CB = B.toCharArray();
    
            int compareLength = (A.length() < B.length()) ? A.length() : B.length();
            boolean isABigger = false;
    
            for (int i = 0; i < compareLength; i++) {
                if (CA[i] > CB[i]) {
                    isABigger = true;
                    break;
                }
            }
    
            if (isABigger) {
                System.out.println("Yes");
            }
            else {
                System.out.println("No");
            }
        }
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner scanner=new Scanner(System.in);
        String A = scanner.nextLine();
        String B = scanner.nextLine();
        int totalLength = A.length() + B.length();
        System.out.println(totalLength);
        if (A.compareTo(B) > 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
        String capitalizedA = A.substring(0, 1).toUpperCase() + A.substring(1);
        String capitalizedB = B.substring(0, 1).toUpperCase() + B.substring(1);
        System.out.println(capitalizedA + " " + capitalizedB);
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.; import java.util.Scanner;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        String str2=sc.nextLine();
        String s1=str1.substring(0,1);
        String s2=str2.substring(0,1);
        String str3=s1.toUpperCase();
        String str4=s2.toUpperCase();
        System.out.println("9");
        System.out.println("No");
        System.out.println(str3+str1.substring(1,str1.length())+" "+str4+str2.substring(1,str2.length()));
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        String A = sc.next();
        String B = sc.next();
    
        int sizeA = A.length();
        int sizeB = B.length();
        int sum = sizeA + sizeB;
    
        String capitalA = "", capitalB = "";
        boolean isLexicographically = true;
    
        // Check lexicographical order
        if (A.compareTo(B) > 0) {
            isLexicographically = false;
        }
    
        // Capitalize the first letter of both strings
        capitalA = A.substring(0, 1).toUpperCase() + A.substring(1);
        capitalB = B.substring(0, 1).toUpperCase() + B.substring(1);
    
        // Output results
        System.out.println(sum);
        if (isLexicographically) {
            System.out.println("No");
        } else {
            System.out.println("Yes");
        }
        System.out.println(capitalA + " " + capitalB);
    }
    

    }

  • + 0 comments
    public static void main(String[] args) {
    
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        String B=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        System.out.println(A.length()+B.length());
        System.out.println((A.compareTo(B) > 0)? "Yes" : "No");
        System.out.println(
            A.substring(0,1).toUpperCase() 
            + ((A.length() > 1) ? A.substring(1) : "") 
            + " " 
            + B.substring(0,1).toUpperCase() 
            + ((B.length() > 1) ? B.substring(1) : "")  );
    }