Group consecutive characters of same type in a string

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

34 FTG 234 ++-*


My Personal Notes arrow_drop_up

Always try to improve and willing to learn

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.