Java Exercises: Rearrange the alphabets in the order followed by the sum of digits
Java Basic: Exercise-192 with Solution
Write a Java program to rearrange the alphabets in the order followed by the sum of digits in a given string containing uppercase alphabets and integer digits (from 0 to 9).
Pictorial Presentation:

Sample Solution:
Java Code:
import java.util.*;
import java.lang.*;
public class Solution {
static final int MAX_CHAR = 20;
public static void main(String args[]) {
String str1 = "AND456HSE8";
System.out.println(arrange_String_nums(str1));
}
static String arrange_String_nums(String str1) {
int char_count[] = new int[MAX_CHAR];
int sum_num = 0;
for (int i = 0; i < str1.length(); i++) {
if (Character.isUpperCase(str1.charAt(i)))
char_count[str1.charAt(i) - 'A']++;
else
sum_num = sum_num + (str1.charAt(i) - '0');
}
String rarr_part = "";
for (int i = 0; i < MAX_CHAR; i++) {
char ch = (char)('A' + i);
while (char_count[i]-- != 0)
rarr_part = rarr_part + ch;
}
if (sum_num > 0)
rarr_part = rarr_part + sum_num;
return rarr_part;
}
}
Sample Output:
ADEHNS23
Flowchart:

Java Code Editor:
Company: Facebook
Contribute your code and comments through Disqus.
Previous: Write a Java program to test whether there are two integers x and y such that x^2 + y^2 is equal to a given positive number.
Next: Write a Java program that accept an integer and find the sum of all the elements from all possible subsets of a set formed by first n natural numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
Java: Anagrams
Generates all anagrams of a string.
public static List<String> anagrams(String input) { if (input.length() <= 2) { return input.length() == 2 ? Arrays.asList(input, input.substring(1) + input.substring(0, 1)) : Collections.singletonList(input); } return IntStream.range(0, input.length()) .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1))) .flatMap(entry -> anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1)) .stream() .map(s -> entry.getValue() + s)) .collect(Collectors.toList()); }
Ref: https://bit.ly/3rvAdAK
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises