0
extracting a string
how can i extract a string? for instance, extract h2pw3t4n into hhpwwwttttn?
1 Resposta
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;
}