Home > front end >  Capitalize first letter of each group of letters in a string
Capitalize first letter of each group of letters in a string

Time:10-11

I've got the following string s:

String s = "abbcccdddd";

I need to capitalize first letters of each group of letters, like:

ABbCccDddd

How can I achieve this?

CodePudding user response:

public static String capFirst(String s){
    
    //String we want to return
    String result = "";
    
    //The first letter will always be capitalized; concatenate it to our result
    result  = s.substring(0, 1).toUpperCase();
    
    //Iterate across original string s
    for (int i = 1; i < s.length(); i  ){
        
        //If the current letter is equal to the previous letter
        if (s.substring(i,i 1).equalsIgnoreCase(s.substring(i-1,i))){
            
            //Concatenate to our return string as is (lower case)
            result  = s.substring(i,i 1);
            
        } else {
            
            //Otherwise, concatenate to our return string as upper case
            result  = s.substring(i,i 1).toUpperCase();
            
        }
    }
    
    //Return final result
    return result;
}

Example

public static void main(String[] args) {
    String s = "abbcccdddd";
    System.out.println(capFirst(s));
}

Output

ABbCccDddd

CodePudding user response:

Using Java 8 groupingBy:

You can implement the same using Java8 groupingBy feature in which you can group the same elements and find the frequency of each element and add it to a list as shown below:

Code:

public class Convert {
    public static void main(String[] args) {
        String s = "abbcccdddd";
        
        Map<Character,Long> countMap = s.chars().mapToObj(x -> (char)x)
            .collect(Collectors.groupingBy(Function.identity(), 
                    LinkedHashMap::new,Collectors.counting()));

        StringBuilder sb = new StringBuilder();
        
        countMap.forEach((k,v) -> {
            for(int i = 0; i < v; i  ){
                if(i==0){
                    sb.append(k.toString().toUpperCase());
                } else{
                    sb.append(k);
                }
            }
        });
        System.out.println(sb);
    }
}

Output:

ABbCccDddd

CodePudding user response:

Just another way. Read the comments in code:

String s = "abbcccdddd";
    
String curLetter = "";   // Holds the current letter being processed in string.
String prevLetter = "";  // Holds the current letter AFTER its been processed.
StringBuilder sb = new StringBuilder("");  // Used to build the string.
    
// Iterate through each character of the string `s`...
for (int i = 0; i < s.length(); i  ) {

    // Get the current letter to process.
    curLetter = String.valueOf(s.charAt(i));

    // Is it the same s the previous letter procesed?
    if (!curLetter.equalsIgnoreCase(prevLetter)) {
        // No, then make the letter Upper Case
        curLetter = curLetter.toUpperCase();
    }
    // Add the current letter to the String Builder
    sb.append(curLetter);
        
    /* Make the prevLetter hold the curLetter for 
       next iteration comparison.      */
    prevLetter = curLetter; 
}
    
// Apply what is in the StringBuilder into a newString variable.
String newString = sb.toString();
    
// Display newString in Console Window.
System.out.println(newString);
  • Related