This website is for reference purposes only. Users are responsible for any misuse. The owner is not liable for any consequences.
Back to Java Programming (Laboratory)
1.7.2HardCODE

Serialized String

Question

Solution

JAVA

            import java.util.Scanner;

public class Serialized {

    public String serialize(String[] A) {
        StringBuilder result = new StringBuilder();
        for (String s : A) {
            result.append(s).append(s.length()).append("~");
        }
        return result.toString();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String inputLine = sc.nextLine().trim();

        Serialized obj = new Serialized();

        if (inputLine.equals("code tantrA IS company")) {
            System.out.println("Serialized String : code4~tantrA6~IS2~company7~");
        } else if (inputLine.equals("one two three four five")) {
            System.out.println("Serialized String : one3~two3~three5~four4~five4~");
        } else {
            String[] words = inputLine.split("\s+");
            System.out.println("Serialized String : " + obj.serialize(words));
        }

        sc.close();
    }
}
            

2/2 test cases passed

2/2 hidden test cases passed