# Split numeric, alphabetic and special symbols from a String

Given a string str, divide the string into three parts one containing a numeric part, one containing alphabetic and one containing special characters.
Examples:

```Input : geeks01for02geeks03!!!
Output :geeksforgeeks
010203
!!!
Here str = "Geeks01for02Geeks03!!!", we scan every character and
append in res1, res2 and res3 string accordingly.

Input : **Docoding123456789everyday##
Output :Docodingeveryday
123456789
**##
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Steps :

1. Calculate the length of the string.
2. Scan each every character(ch) of a string one by one
• if (ch is a digit) then append it in res1 string.
• else if (ch is alphabet) append in string res2.
• else append in string res3.
3. Print the all the strings, we will have one string containing numeric part, other non numeric part and last one contain special characters.

## C++

 `// CPP program to split an alphanumeric ` `// string using STL ` `#include ` `using` `namespace` `std; ` ` `  `void` `splitString(string str) ` `{ ` `    ``string alpha, num, special; ` `    ``for` `(``int` `i=0; i= ``'A'` `&& str[i] <= ``'Z'``) || ` `                ``(str[i] >= ``'a'` `&& str[i] <= ``'z'``)) ` `            ``alpha.push_back(str[i]); ` `        ``else` `            ``special.push_back(str[i]); ` `    ``} ` ` `  `    ``cout << alpha << endl; ` `    ``cout << num << endl; ` `    ``cout << special << endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeks01\$\$for02geeks03!@!!"``; ` `    ``splitString(str); ` `    ``return` `0; ` `} `

## Java

 `// CPP program to split an alphanumeric ` `// string using stringbuffer ` ` `  `class` `Test ` `{ ` `    ``static` `void` `splitString(String str) ` `    ``{ ` `        ``StringBuffer alpha = ``new` `StringBuffer(),  ` `        ``num = ``new` `StringBuffer(), special = ``new` `StringBuffer(); ` `         `  `        ``for` `(``int` `i=``0``; i

## Python3

 `# Python 3 program to split an alphanumeric ` `# string using STL ` `def` `splitString(``str``): ` ` `  `    ``alpha ``=` `"" ` `    ``num ``=` `"" ` `    ``special ``=` `"" ` `    ``for` `i ``in` `range``(``len``(``str``)): ` `        ``if` `(``str``[i].isdigit()): ` `            ``num ``=` `num``+` `str``[i] ` `        ``elif``((``str``[i] >``=` `'A'` `and` `str``[i] <``=` `'Z'``) ``or` `             ``(``str``[i] >``=` `'a'` `and` `str``[i] <``=` `'z'``)): ` `            ``alpha ``+``=` `str``[i] ` `        ``else``: ` `            ``special ``+``=` `str``[i] ` ` `  `    ``print``(alpha) ` `    ``print``(num ) ` `    ``print``(special) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``str` `=` `"geeks01\$\$for02geeks03!@!!"` `    ``splitString(``str``) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to split an alphanumeric ` `// string using stringbuffer ` `using` `System; ` `using` `System.Text;  ` ` `  `class` `GFG  { ` `     `  `    ``// Fucntion ot split string ` `    ``static` `void` `splitString(``string` `str) ` `    ``{ ` `        ``StringBuilder alpha =  ` `                 ``new` `StringBuilder(); ` `        ``StringBuilder num =  ` `                 ``new` `StringBuilder(); ` `        ``StringBuilder special =  ` `                 ``new` `StringBuilder(); ` `     `  `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``{ ` `            ``if` `(Char.IsDigit(str[i])) ` `                ``num.Append(str[i]); ` `            ``else` `if``((str[i] >= ``'A'` `&&  ` `                     ``str[i] <= ``'Z'``) ||  ` `                     ``(str[i] >= ``'a'` `&&  ` `                      ``str[i] <= ``'z'``)) ` `                ``alpha.Append(str[i]); ` `            ``else` `                ``special.Append(str[i]); ` `        ``} ` `     `  `        ``Console.WriteLine(alpha); ` `        ``Console.WriteLine(num); ` `        ``Console.WriteLine(special); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``string` `str = ``"geeks01\$\$for02geeks03!@!!"``; ` `        ``splitString(str); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

Output:

```geeksforgeeks
010203
\$\$!@!!
```

Time complexity of above solution is O(n) where n is the length of the string.

This article is contributed by Rishabh Jain. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : Sam007, chitranayal

Article Tags :
Practice Tags :

2

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