Java Exercises: Create a basic string compression method using the counts of repeated characters
Java Basic: Exercise-208 with Solution
Write a Java program to create a basic string compression method using the counts of repeated characters.
Sample Solution:
Java Code:
import java.util.Scanner;
public class StringCompression
{
public static void main(String[] args)
{
StringCompression str = new StringCompression();
String s1,s2;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string (you can include space as well)");
s1 = in.nextLine();
//Trim all the spaces of the string using replaceAll method
s2 = s1.replaceAll("\\s","");
str.Compression(s2);
}
//Create a Java Method Compression to compress the string
public static String Compression(String s)
{
int count = 1;
StringBuilder sb = new StringBuilder();
//Below for loop counts all characters of the string apart from the last one
//The last character won't get appended by the class StringBuilder here as it
//does not enter the for loop once the length completes the count
for (int i = 1; i < s.length()-1 ; i++)
{
if (s.charAt(i) == s.charAt(i - 1))
{
count++;
}
else
{
sb.append(s.charAt(i - 1));
sb.append(count);
count = 1;
}
}
//Count the last character of the string
if (s.length() > 1)
{
//Compare the last two characters of the string
if (s.charAt(s.length() - 1) == s.charAt(s.length() - 2))
{
count++;
}
else
{
sb.append(s.charAt(s.length() - 2));
sb.append(count);
count = 1;
}
sb.append(s.charAt(s.length() - 1));
sb.append(count);
}
s = sb.toString();
System.out.println("The compressed string along with the counts of repeated characters is:" + "\n" +s);
return s;
}
}
Sample Output:
Enter a string (you can include space as well) aaaabbbbcccccddddeeee The compressed string along with the counts of repeated characters is: a4b4c5d4e4
Pictorial Presentation:
Flowchart 1:
Flowchart 2:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to merge two sorted (ascending) linked lists in ascending order.
Next: Write a Java program to find all unique combinations from a collection of candidate numbers. The sum of the numbers will be equal to a given target number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework