Question

Write a routine which will take a string of characters consisting of just the letters A B and C and rearrange the string so that at the end of the code

In Java

Write a routine which will take a string of characters consisting of just the letters A B and C and rearrange the string so that at the end of the code there will first be all the "A" 's then all the "B" 's and then all the "C" 's. Example string "ACBBACABC" should end up "AAABBBCCC"


1 0
Add a comment Improve this question Transcribed image text
Answer #1
package com.company;
import java.util.*;
public class Main {

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

        System.out.print("Enter a string that only contains A, B or C: ");
        String str = sc.nextLine();

        String[] letters = str.split("");

        for (int i = 0; i < letters.length; i++) {
            if (letters[i].equals("A") || letters[i].equals("B") || letters[i].equals("C")) {
                continue;
            }
            else {
                System.exit(-1);
            }
        }

        /* calling function to sort */
        convert(letters);

        /* displaying result */
        for (int i = 0; i < letters.length; i++) {
            System.out.print(letters[i]);
        }
    }

    /* method for sorting string in ascending with all A's at first then all B's and then C's */
    public static String[] convert(String[] str) {
        String temp;
        for (int i = 0; i < str.length; i++) {
            for (int j = 0; j < str.length; j++) {

                /* comparing characters and swapping their positions if compareTo is not 0 */
                if (str[i].compareTo(str[j]) < 0) {
                    temp = str[j];
                    str[j] = str[i];
                    str[i] = temp;
                }
            }
        }

        /* returning sorted list */
        return str;
    }
}

For help please comment.
Thank You

Add a comment
Know the answer?
Add Answer to:
Write a routine which will take a string of characters consisting of just the letters A B and C and rearrange the string so that at the end of the code
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in java to read a string object consisting 300 characters or more using...

    Write a program in java to read a string object consisting 300 characters or more using index input Stream reader. The program should perform following operations. The String must have proper words and all kind of characters.(Use String class methods). a, Determine the length of the string count the number of letters in the strings , count the number of numeric object d. Calculate the number of special character e. Compute the ratio of the numeric to the total f....

  • Do in Python Problem 2 Assume s is a string of lower case characters. Write a...

    Do in Python Problem 2 Assume s is a string of lower case characters. Write a program that prints the number of times the string ' lol' occurs in s For example, if s= 'azclololegghakl', then your program should print S Number of times lol occu rs is: 2 Problem 3 Assume s is a string of lower case characters Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example,...

  • You will be given several strings full of different keyboard characters, some of which are letters...

    You will be given several strings full of different keyboard characters, some of which are letters of the alphabet. Write a java program that creates a binary tree that uses only the alphabetical characters (a-z, A-Z) as the value within the leaf nodes using recursion and preorder traversal. All other values within the tree (either the root node or internal nodes) will be null. You may create any methods you see fit, so long as the final binary tree is...

  • Counting subsets 4. (a). How many ways are there to rearrange (all of) the letters of SPIDERMAN so that all of the consonants appear in alphabetical order (not necessarily consec- utively)? (b). How...

    Counting subsets 4. (a). How many ways are there to rearrange (all of) the letters of SPIDERMAN so that all of the consonants appear in alphabetical order (not necessarily consec- utively)? (b). How many ways are there to rearrange (all of) the letters of SPIDERMAN so that the word PRIDE appears together as a word somewhere? (c). How many ways are there to rearrange (all of) the letters of SPIDERMAN that begin and end with a vowel? (d). How many...

  • Write a Java program that that capitalizes all first letters of the words in the given...

    Write a Java program that that capitalizes all first letters of the words in the given String. All other symbols should be intact. If a word does not start with a letter, it should remain intact as well. Assume that the parameter String can only contain spaces and alphanumeric characters. Example: Input: “100 234 jus 23 jskl” Output: “100 234 Jus23 jskl”

  • Write a C program that prompts the user for an input string with all lowercase letters....

    Write a C program that prompts the user for an input string with all lowercase letters. Your program should then sort the characters in the string and print the sorted string. Assume that the string contains no spaces and has at most 30 lowercase characters. Your program should loop continually until the user enters “q” to quit.

  • Lab2: Processing Strings Part#1 – Counting Vows Assume s is a string of lower case characters....

    Lab2: Processing Strings Part#1 – Counting Vows Assume s is a string of lower case characters. Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', and 'u'. For example, if s = 'azcbobobegghakl', your program should print: Number of vowels: 5 Part#2 – Counting Bobs Assume s is a string of lower case characters. Write a program that prints the number of times the string 'bob' occurs...

  • in Java please (a) Write a static method abbreviate( ) which is passed a String s...

    in Java please (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .). For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too...

  • Write a C program to “de-vowel” an input string. Assume that the user provides input containing...

    Write a C program to “de-vowel” an input string. Assume that the user provides input containing only the characters a through z (i.e., all lowercase letters). Your program should create an output string that deletes all vowels from the input string, pushing the letters together to fill any gaps. For example, given the input “theturtleandthehare” your code should print out “thtrtlndthhr”. Your program should create an output string from the input string, before printing its output. Sample Input: Enter a...

  • Write a C function first_last that consumes a string argument and modifies it so that it...

    Write a C function first_last that consumes a string argument and modifies it so that it only contains two characters the first, and the last. If there are less than two characters, leave the string alone. Return nothing For example: Test Result char str[]="hello"; first_last(str) printf("%s", str); ho

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT