extracting a string | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

extracting a string

how can i extract a string? for instance, extract h2pw3t4n into hhpwwwttttn?

19th Dec 2016, 4:16 PM
Matin Grimes
Matin Grimes - avatar
1 Respuesta
0
quick&dirty this should work as an example #include <iostream> #include <stdlib.h> using namespace std; int main() { string s = "h2pw3t4n"; string snum = ""; string sout = ""; char c = '\0'; cout << "in: " << s << endl; for (int i = 0; i < s.length(); ++i) { if (isalpha(s[i])) { if (c != '\0') { if (snum.length() != 0) { for (int j = 0; j < atoi(snum.c_str()); ++j) { sout += c; } snum = ""; } else sout += c; } c = s[i]; if (i == s.length()-1) { sout += c; } } else if (isdigit(s[i])) { snum += s[i]; } } cout << "out: " << sout << endl; return 0; }
20th Dec 2016, 2:07 AM
marcy learner