How can I insert characters into a string?
Example:Code:{R5;;;;1;;1;} ---> {R5;0;0;0;1;0;1;}
How can I insert characters into a string?
Example:Code:{R5;;;;1;;1;} ---> {R5;0;0;0;1;0;1;}
If I´m not wrong it´s like this
Code:int main() { char aname[] = "Steve"; string name = aname; cout << name << endl; return 0; }
Or do you mean one character at time???
Code:int main() { char aname[] = "Steve"; string name; int size = sizeof(aname) / sizeof(char); for (int i = 0; i < (size -1); i++) name +=aname[i]; cout << name << endl; return 0; }
@ ripper079:
I wanted to convert "C1;;;4;;1;" into something that looks like this string: "C1;0;0;4;0;1;".
I tried strtok() but it just removes the ";" and returns: "C1 4 1".
Is there a way to read the string up to a certain character (in this case ";")? After this I need some function to insert the "0" after this position.
Any Idea?
Thank You.
>I wanted to convert "C1;;;4;;1;" into something that looks like this string: "C1;0;0;4;0;1;".
This is hasty, but it should work:
-PreludeCode:#include <iostream> #include <string> #include <vector> template <typename Container, typename ValueType> Container insert_val ( Container orig, ValueType val, ValueType rep ) { std::vector<char> dest; std::vector<char>::iterator it = orig.begin(); while ( it != orig.end() ) { dest.push_back ( *it ); if ( *it == val && *(it + 1) == val ) dest.push_back ( rep ); ++it; } return Container ( dest.begin(), dest.end() ); } int main() { std::string broken = "C1;;;4;;1;"; std::string fixed = insert_val ( broken, ';', '0' ); std::cout<< broken <<std::endl; std::cout<< fixed <<std::endl; return 0; }
My best code is written with the delete key.
Works great. Thanks a lot