0

Copying one string to another ignoring some characters.

Hello Everyone, The idea is to get a user input in the form of a string representing a money amount like “$1,234,567.45" and then copy it into another char array ignoring the "

quot; sign and the "commas". I am not sure what is probably wrong with this code. your reply means alot. Code: #include <iostream> #include <string> #include <cstring> using namespace std; int main() { string Money; char Mon[50]; cin>>Money; for (int i=1; i<Money.length();i++) { cout<<Money.at(i); if (Money[i] != '\44') { Mon[i]=Money[i]; } else {} } cout<<Mon; return 0; }

3rd Feb 2020, 2:59 PM
Sajid Naseeb
Sajid Naseeb - avatar
3 Réponses
+ 8
Imagine, if comma found at index 6 but, your character array Mon[50] have already stored only 3 elements but, it will skip to index "i" and stores given value, this is what mistake you did. Solution: Make saparate variable to assign values to another array. int index = 0; for(size_t i = 0; i<M.length(); i++){ if(M[i]!='
#x27;&&M[i]!=','){ Mon[index] = M[i]; // & then, increase the value of "index" for next element to store. index++; } } Hope this helps!
4th Feb 2020, 4:59 PM
Vinay_GB
Vinay_GB - avatar
+ 1
thank you very much.
2nd Apr 2020, 10:19 PM
Sajid Naseeb
Sajid Naseeb - avatar
0
I haven't used it in c++ yet however what you're looking for is Regular Expression or RegEx In java (for example) you can filter out symbols, letters or numbers using regex Sample: String reg = "$600.77"; String out = reg.replaceAll("[$ \.]", ""); System.out.print(reg); Output: 60077 Try this link: https://regular-expressions.mobi/stdregex.html?wlr=1
3rd Feb 2020, 3:14 PM
HNNX 🐿
HNNX 🐿 - avatar