Java Strings Introduction

Sort by

recency

|

1256 Discussions

|

  • + 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. */
    
        int length = A.length()+B.length();
        System.out.println(length);
        if(A.compareTo(B)>0 )
          System.out.println("Yes");
          else
          System.out.println("No");
    
        System.out.println(A.substring(0,1).toUpperCase()+A.substring(1)
        +" "+B.substring(0,1).toUpperCase()+B.substring(1));
    
    
    
    }
    
  • + 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();
    
        System.out.println(A.length() + B.length());
    
        if (A.compareTo(B) > 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    
        String AB = A + " " + B;
        String[] split = AB.split(" ");
        StringBuilder sb = new StringBuilder();
    
        for (String s : split) {
            char upper = Character.toUpperCase(s.charAt(0));
            sb.append(upper).append(s.substring(1)).append(" ");
        }
        System.out.println(sb);
    }
    

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String a = scan.nextLine();
            String b = scan.nextLine();
    
            System.out.println(a.length() + b.length());
    
            if (a.compareTo(b) > 0) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
    
            String aCapitalized = a.substring(0, 1).toUpperCase() + a.substring(1);
            String bCapitalized = b.substring(0, 1).toUpperCase() + b.substring(1);
    
            System.out.println(aCapitalized + " " + bCapitalized);
        }
    }
    
  • + 0 comments

    Hi this is the code i submitted and it passed all test cases, but i am confused as to why this line worked (A.compareTo(B) > 1). When i used the line A.compareTo(B) == 1 test case two kept on failing until i made this change by mistake i meant to write 0 (zero) instead of 1 (one).

    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();
        sc.close();
    
        System.out.println(A.length() + B.length());
    
        if(A.compareToIgnoreCase(B) > 1)
                System.out.println("Yes");
            else
                System.out.println("No");
    
        System.out.println(
            A.replace(A.charAt(0), Character.toUpperCase(A.charAt(0)))
            +" "+ B.replace(B.charAt(0), Character.toUpperCase(B.charAt(0))));
    
    }
    

    }

  • + 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();
        /* Enter your code here. Print output to STDOUT. */
        //first line, sum the lengths of A and B
        int lengthOfAB = A.length() + B.length();
        System.out.println(lengthOfAB); 
    
        //second line, write "Yes" if A is lexicographically greater than B otherwise "No"
        int compareTo = A.compareToIgnoreCase(B);
        if(compareTo != 0){
            System.out.println("No");
        } else {
            System.out.println("Yes");
        }
    
        //third line, capitalize the first letter in both A and B and print them on a single ine seperated by a space
        //I want to grab the first letter from A and B no matter the input and capatalize the input but also print the rest of the string with a space seperated between them 
        char aCapitalize = A.charAt(0);
        char bCapitalize = B.charAt(0);
        String aStringCapitalize = String.valueOf(aCapitalize).toUpperCase();
        String bStringCapitalize = String.valueOf(bCapitalize).toUpperCase();
        System.out.print(aStringCapitalize + A.substring(1) + " " + bStringCapitalize + B.substring(1));
    
        //test case 1 
        //input : java 
        //        java
        //Expected output
        //8
        //No
        //Java Java
        //Test Case 2
        //input: java
        //       hello
        //Expected output
        //9
        //Yes
        //Java Hello
        //both test cases work but results do fail from hackerank not sure why put custom test case and passes both 
        //code by: @jesse_aluiso
    
    }
    

    }