Skip to content
Related Articles

Related Articles

Improve Article

Group consecutive characters of same type in a string

  • Difficulty Level : Easy
  • Last Updated : 27 May, 2021

Given a string str consisting of upper case alphabets, numeric digits and arithmetic operators, the task is to group them into continuous sub-strings of same type.
Examples: 
 

Input: str = “G E E K S 1 2 3 4 5” 
Output: GEEKS 12345 
All contiguous upper case characters can be grouped together 
and all the numeric characters can be grouped together in a separate group.
Input: str = “DEGFT +- * 5 6 7” 
Output: DEGFT +-* 567 
 

 

Approach: Remove all the spaces from string. After removing all the white spaces, traverse the string one by one and group the characters according to their ASCII values.
Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the modified string
string groupCharacters(string s, int len)
{
 
    // Store original string
    string temp = "";
 
    // Remove all white spaces
    for (int i = 0; i < len; i++)
        if (s[i] != ' ')
            temp = temp + s[i];
 
    len = temp.length();
 
    // To store the resultant string
    string ans = "";
    int i = 0;
 
    // Traverse the string
    while (i < len) {
 
        // Group upper case characters
        if (int(temp[i]) >= int('A')
            && int(temp[i]) <= int('Z')) {
            while (i < len && int(temp[i]) >= int('A')
                   && int(temp[i]) <= int('Z')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
 
        // Group numeric characters
        else if (int(temp[i]) >= int('0')
                 && int(temp[i]) <= int('9')) {
            while (i < len && int(temp[i]) >= int('0')
                   && int(temp[i]) <= int('9')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
 
        // Group arithmetic operators
        else {
            while (i < len && int(temp[i]) >= int('*')
                   && int(temp[i]) <= int('/')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    }
 
    // Return the resultant string
    return ans;
}
 
// Driver code
int main()
{
    string s = "34FTG234+ +- *";
    int len = s.length();
    cout << groupCharacters(s, len);
    return 0;
}

Java




// Java implementation of the approach
class GFG {
 
  
// Function to return the modified String
static String groupCharacters(char []s, int len)
{
  
    // Store original String
    String temp = "";
  
    // Remove all white spaces
    for (int i = 0; i < len; i++)
        if (s[i] != ' ')
            temp = temp + s[i];
  
    len = temp.length();
  
    // To store the resultant String
    String ans = "";
    int i = 0;
  
    // Traverse the String
    while (i < len) {
  
        // Group upper case characters
        if (temp.charAt(i) >= ('A')
            && temp.charAt(i) <= ('Z')) {
            while (i < len && temp.charAt(i) >= ('A')
                   && temp.charAt(i) <= ('Z')) {
                ans = ans + temp.charAt(i);
                i++;
            }
            ans = ans + " ";
        }
  
        // Group numeric characters
        else if (temp.charAt(i) >= ('0')
                 && temp.charAt(i) <= ('9')) {
            while (i < len && temp.charAt(i) >= ('0')
                   && temp.charAt(i) <= ('9')) {
                ans = ans + temp.charAt(i);
                i++;
            }
            ans = ans + " ";
        }
  
        // Group arithmetic operators
        else {
            while (i < len && temp.charAt(i) >= ('*')
                   && temp.charAt(i) <= ('/')) {
                ans = ans + temp.charAt(i);
                i++;
            }
            ans = ans + " ";
        }
    }
  
    // Return the resultant String
    return ans;
}
  
// Driver code
public static void main(String[] args) {
   String s = "34FTG234+ +- *";
    int len = s.length();
    System.out.print(groupCharacters(s.toCharArray(), len));
    }
}
// This code contributed by Rajput-Ji

Python 3




# Python 3 implementation of the approach
 
# Function to return the modified string
def groupCharacters(s, l):
 
    # Store original string
    temp = ""
 
    # Remove all white spaces
    for i in range(l):
        if (s[i] != ' '):
            temp = temp + s[i]
 
    l = len(temp)
 
    # To store the resultant string
    ans = ""
    i = 0
 
    # Traverse the string
    while (i < l):
 
        # Group upper case characters
        if (ord(temp[i]) >= ord('A')
            and ord(temp[i]) <= ord('Z')):
            while (i < l and ord(temp[i]) >= ord('A')
                and ord(temp[i]) <= ord('Z')) :
                ans = ans + temp[i]
                i += 1
     
            ans = ans + " "
 
        # Group numeric characters
        elif (ord(temp[i]) >= ord('0')
                and ord(temp[i]) <= ord('9')):
            while (i < l and ord(temp[i]) >= ord('0')
                and ord(temp[i]) <= ord('9')):
                ans = ans + temp[i]
                i += 1
            ans = ans + " "
 
        # Group arithmetic operators
        else :
            while (i < l and ord(temp[i]) >= ord('*')
                and ord(temp[i]) <= ord('/')):
                ans = ans + temp[i]
                i += 1
            ans = ans + " "
 
    # Return the resultant string
    return ans
 
# Driver code
if __name__ == "__main__":
     
    s = "34FTG234+ +- *"
    l = len(s)
    print(groupCharacters(s, l))
 
# This code is contributed by Ita_c

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the modified String
    static String groupCharacters(char []s, int len)
    {
     
        // Store original String
        string temp = "";
     
        // Remove all white spaces
        for (int j = 0; j < len; j++)
            if (s[j] != ' ')
                temp = temp + s[j];
     
        len = temp.Length;
     
        // To store the resultant String
        string ans = "";
        int i = 0;
     
        // Traverse the String
        while (i < len)
        {
     
            // Group upper case characters
            if (temp[i] >= ('A')
                && temp[i] <= ('Z'))
            {
                while (i < len && temp[i] >= ('A')
                    && temp[i] <= ('Z'))
                {
                    ans = ans + temp[i];
                    i++;
                }
                ans = ans + " ";
            }
     
            // Group numeric characters
            else if (temp[i] >= ('0')
                    && temp[i] <= ('9'))
            {
                while (i < len && temp[i] >= ('0')
                    && temp[i] <= ('9'))
                {
                    ans = ans + temp[i];
                    i++;
                }
                ans = ans + " ";
            }
     
            // Group arithmetic operators
            else
            {
                while (i < len && temp[i] >= ('*')
                    && temp[i] <= ('/'))
                {
                    ans = ans + temp[i];
                    i++;
                }
                ans = ans + " ";
            }
        }
     
        // Return the resultant String
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        string s = "34FTG234+ +- *";
        int len = s.Length;
        Console.WriteLine(groupCharacters(s.ToCharArray(), len));
    }
}
 
// This code contributed by Ryuga

PHP




<?php
// PHP implementation of the approach
 
// Function to return the modified string
function groupCharacters($s, $len)
{
 
    // Store original string
    $temp = "";
 
    // Remove all white spaces
    for ($i = 0; $i < $len; $i++)
        if ($s[$i] != ' ')
            $temp = $temp.$s[$i];
 
    $len = strlen($temp);
 
    // To store the resultant string
    $ans = "";
    $i = 0;
 
    // Traverse the string
    while ($i < $len)
    {
 
        // Group upper case characters
        if (ord($temp[$i]) >= ord('A') &&
            ord($temp[$i]) <= ord('Z'))
        {
            while ($i < $len && ord($temp[$i]) >= ord('A') &&
                                ord($temp[$i]) <= ord('Z'))
            {
                $ans = $ans.$temp[$i];
                $i++;
            }
            $ans = $ans . " ";
        }
 
        // Group numeric characters
        else if (ord($temp[$i]) >= ord('0') &&
                 ord($temp[$i]) <= ord('9'))
        {
            while ($i < $len && ord($temp[$i]) >= ord('0') &&
                                ord($temp[$i]) <= ord('9'))
            {
                $ans = $ans.$temp[$i];
                $i++;
            }
            $ans = $ans . " ";
        }
 
        // Group arithmetic operators
        else
        {
            while ($i < $len && ord($temp[$i]) >= ord('*') &&
                                ord($temp[$i]) <= ord('/'))
            {
                $ans = $ans.$temp[$i];
                $i++;
            }
            $ans = $ans . " ";
        }
    }
 
    // Return the resultant string
    return $ans;
}
 
// Driver code
$s = "34FTG234+ +- *";
$len = strlen($s);
print(groupCharacters($s, $len));
     
// This code is contributed by mits
?>

Javascript




<script>
// Javascript implementation of the approach
 
// Function to return the modified String
function groupCharacters(s,len)
{
    // Store original String
    let temp = "";
    
    // Remove all white spaces
    for (let i = 0; i < len; i++)
        if (s[i] != ' ')
            temp = temp + s[i];
    
    len = temp.length;
    
    // To store the resultant String
    let ans = "";
    let i = 0;
    
    // Traverse the String
    while (i < len) {
    
        // Group upper case characters
        if (temp[i] >= ('A')
            && temp[i] <= ('Z')) {
            while (i < len && temp[i] >= ('A')
                   && temp[i] <= ('Z')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    
        // Group numeric characters
        else if (temp[i] >= ('0')
                 && temp[i] <= ('9')) {
            while (i < len && temp[i] >= ('0')
                   && temp[i] <= ('9')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    
        // Group arithmetic operators
        else {
            while (i < len && temp[i] >= ('*')
                   && temp[i] <= ('/')) {
                ans = ans + temp[i];
                i++;
            }
            ans = ans + " ";
        }
    }
    
    // Return the resultant String
    return ans;
}
 
// Driver code
let s = "34FTG234+ +- *";
let len = s.length;
document.write(groupCharacters(s.split(""), len));
 
 
         
// This code is contributed by rag2127
</script>
Output: 
34 FTG 234 ++-*

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :