Dada la string str, divida la string en tres partes, una que contenga una parte numérica, otra que contenga caracteres alfabéticos y otra que contenga caracteres especiales.
Ejemplos:
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 **##
Pasos :
- Calcula la longitud de la cuerda.
- Escanea cada carácter (ch) de una string uno por uno
- si (ch es un dígito), agréguelo en la string res1.
- de lo contrario, si (ch es el alfabeto) se agrega en la string res2.
- de lo contrario, agregue en la string res3.
- Imprima todas las strings, tendremos una string que contiene una parte numérica, otra parte no numérica y la última contiene caracteres especiales.
Implementación:
C++
// CPP program to split an alphanumeric // string using STL #include<bits/stdc++.h> using namespace std; void splitString(string str) { string alpha, num, special; for (int i=0; i<str.length(); i++) { if (isdigit(str[i])) num.push_back(str[i]); else if((str[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
// java 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<str.length(); i++) { if (Character.isDigit(str.charAt(i))) num.append(str.charAt(i)); else if(Character.isAlphabetic(str.charAt(i))) alpha.append(str.charAt(i)); else special.append(str.charAt(i)); } System.out.println(alpha); System.out.println(num); System.out.println(special); } // Driver method public static void main(String args[]) { String str = "geeks01$$for02geeks03!@!!"; splitString(str); } }
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 { // Function 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
Javascript
<script> // Javascript program to split an alphanumeric // string using stringbuffer function splitString(str) { let alpha = ""; let num = ""; let special = ""; for (let i=0; i<str.length; i++) { if (!isNaN(String(str[i]) * 1)) num+=str[i]; else if((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) alpha+=str[i]; else special+=str[i]; } document.write(alpha+"<br>"); document.write(num+"<br>"); document.write(special+"<br>"); } // Driver method let str = "geeks01$$for02geeks03!@!!"; splitString(str); // This code is contributed by avanitrachhadiya2155 </script>
Producción
geeksforgeeks 010203 $$!@!!
La complejidad temporal de la solución anterior es O(n) donde n es la longitud de la string.
Espacio auxiliar: O(n), donde n es la longitud de la string.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA