Given a string str, divide the string into three parts one containing a numeric part, one containing alphabetic and one containing special characters.
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 **##
- Calculate the length of the string.
- 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.
- Print the all the strings, we will have one string containing numeric part, other non numeric part and last one contain special characters.
# Python 3 program to split an alphanumeric
# string using STL
alpha = “”
num = “”
special = “”
for i in range(len(str)):
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 [tabby title="C#"]
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 email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count Uppercase, Lowercase, special character and numeric values
- Print all the palindromic permutations of given string in alphabetic order
- Splitting a Numeric String
- Python Regex to extract maximum numeric value from a string
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Move all special char to the end of the String
- Count special palindromes in a String
- Check if a string can be rearranged to form special palindrome
- Program to count vowels, consonant, digits and special characters in string.
- Split the string into substrings using delimiter
- How to split a string in C/C++, Python and Java?
- Check if given string can be split into four distinct strings
- Ways to split string such that each partition starts with distinct character
- WildCard pattern matching having three symbols ( * , + , ? )