0

How to extract a string

how can i extract a string? for instance, extract h2pw3t4n into hhpwwwttttn? for any example like this, how can i do this? pls help me, its so important.

24th Dec 2016, 12:56 PM
Matin Grimes
Matin Grimes - avatar
10 Respostas
+ 7
Let's say you have a string "h2pw3t4n". string x = "h2pw3t4n"; int z = x.length(); char character[z] = x.c_str(); string str[z]; for (int i = 0; i < z; i++) { string temp (character[i]); str[i] = temp; i++; } for (int j = 0; j < z; j++) { if (str[j] == "2") { str[j] = "h"; } if (str[j] == "3") { str[j] = "ww"; } if (str[j] == "4") { str[j] = "ttt"; } } for (int k = 0; k < z; k++) { cout << str[k]; }
24th Dec 2016, 4:29 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
You don't need to determine which is letter and which is numeric value since my code stores everything into string arrays and evaluate the strings using if... else statements to see if they require conversion/extraction or not. If you want to convert/extract numeric values to string, let the if... else handle the work. The downside is that a lot of if... else statements are needed, and if you want your conversion/extraction to be based on the surrounding letters, it may require a tweak on the code.
24th Dec 2016, 4:35 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
You will need to list out your preferred conversion/extraction condition, in order for me to write the complete version of the code. e.g. 1 to a 2 to bb etc.
25th Dec 2016, 12:53 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
read each character of the source string. if it is a letter, add to the (separate) destination string. if it is a number repeat adding the last letter to the destination string as many times as the number indicates.
24th Dec 2016, 1:09 PM
ifl
ifl - avatar
+ 1
this is a good way but how can i tell the compiler to determine which is letter and which is nember in a string?
24th Dec 2016, 1:26 PM
Matin Grimes
Matin Grimes - avatar
+ 1
you know a character is a digit if its ASCII value is between that of '0' and '9' ant take advantage of automatic casting between char and int. char c; // ... initialise c here if ( c >= '0' && c <= '9'){ // c is a digit int val = c - '0'; }
24th Dec 2016, 2:00 PM
ifl
ifl - avatar
+ 1
tnx my friend, it worked. but i still cant write the right code to extract my string i tried but im terrible in coding if you write me the code of extracting a string that would be graet because i really need it.
24th Dec 2016, 2:40 PM
Matin Grimes
Matin Grimes - avatar
+ 1
tnx, but i need a code to work for all strings like that can you write me the code?
24th Dec 2016, 4:32 PM
Matin Grimes
Matin Grimes - avatar
0
i published some example in the code section. search for 'expand string v1.0'
25th Dec 2016, 11:03 AM
ifl
ifl - avatar
0
use this code. store the string to extract in the str variable. extracted string is generated in the exstr variable. works up to 9 character repetitions. str = 'hp3w3tn5' #expected = 'hpppwwwtnnnnn' exstr = '' for c in range(len(str)): if str[c].isdigit(): exstr += str[c-1]*(int(str[c])-1) else: exstr += str[c] print('extracted :', exstr)
26th Jan 2017, 9:08 AM
pravees ivanovich