Replacing all instances of one ascii character with another in c++? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace all occurrences of a character in string?
E.g, I have a string, "Hello World" and I want to replace all the "l"s with "1"s. How would I do this? I am new to c++. Most of my background is in Python in which you could just use the .replace method.

Use std::replace.
#include <string>
#include <algorithm>
#include <iostream>
int main() {
std::string str = "Hello World";
std::replace(str.begin(),str.end(),'l','1');
std::cout << str; //He11o Wor1d
}

Related

How to convert int to char in c++ style? [duplicate]

This question already has answers here:
Easiest way to convert int to string in C++
(30 answers)
Closed 2 years ago.
Please tell me how to convert int to char in c++ style? The content in str1 is "\001", while the content in str2 is "1"; Can I use static_cast to get the same result as str2?
The code is as follows:
#include <iostream>
using namespace std;
int main()
{
int a = 1;
string str1;
string str2;
str1.push_back(static_cast<char>(a));
str2.push_back('0' + a);
cout << str1 << str2;
return 0;
}
It depends on the exact use-case. For a direct int-to-string conversion, use std::to_string.
When streaming the output, there are usually better options, including using fmt (part of C++20, available as a separate library before that).

Convert large double to scientific notation for use as string? [duplicate]

This question already has answers here:
C++: Converting a double/float to string, preserve scientific notation and precision
(2 answers)
Closed 5 years ago.
This question is very clearly in no way an "exact duplicate" of the marked question, and that post does not address my goal.
If I have:
double yuge = 1e16;
And I try to add it to a string like this:
std::string boogers = std::to_string (yuge) + ".csv";
I get a file name of 100000000000000000.csv.
I want a nice compact version like 1e16.csv.
As you can see, I would like to use it as a file name, so output methods arent helpful. Halp! Thanks.
you can use std::stringstream to construct the string instead of + operator.
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
int main()
{
double youge = 1e16;
std::stringstream ss;
ss<<youge<<".csv";
std::string filename = ss.str(); // filename = 1e+16.csv
filename.erase(std::remove(filename.begin(), filename.end(), '+'), filename.end());// removing the '+' sign
std::cout<<filename; // 1e16.csv
}

Get a text from a binary vector [duplicate]

This question already has answers here:
How to construct a std::string from a std::vector<char>?
(7 answers)
Closed 7 years ago.
I have a
std::vector<unsigned char> data;
which contains a binary read file.
If I write it to
std::ofstream outputFile("file", std::ios_base::binary);
I'll see a regular text in it. Then I can read it into std::string which will conatain the text.
Is it possible to copy vector directly to string with the same result?
Use the 6th form of std::string's constructor:
// Example program
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<unsigned char> data{'a', 'b', 'c'};
std::string str(data.begin(), data.end());
std::cout << str << std::endl;
}
Live demo here
From http://www.cplusplus.com/reference/string/string/string/.
Use std::String s(data.begin(), data.end());.

how to tokenize a string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
hello every one i want to divide my string into two parts based on '\t' is there any built in function i tried strtok but it take char * as first in put but my variable is of type string
thanks
#include <sstream>
#include <vector>
#include <string>
int main() {
std::string str("abc\tdef");
char split_char = '\t';
std::istringstream split(str);
std::vector<std::string> token;
for(std::string each; std::getline(split, each, split_char); token.push_back(each));
}
Why can't you use C standard library?
Variant 1.
Use std::string::c_str() function to convert a std::string to a C-string (char *)
Variant 2.
Use std::string::find(char, size_t) to find a necessary symbol ('\t' in your case) than make a new string with std::string::substr. Loop saving a 'current position' till the end of line.

Splitting a line in C/C++ using whitespace as delimiter [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
pseudocode:
Attributes[] = Split line(' ')
How?
I have been doing this:
char *pch;
pch = strtok(line," ");
while(pch!=NULL)
{
fputs ( pch, stdout );
}
and getting a non-written, stuck, exit file. It's something wrong with this?
Well, the thing isn't even meeting my pseudocode requirement, but I'm confused about how to index tokens (as char arrays) to my array, I guess I should write a 2-dim array?
Use strtok with " " as your delimiter.
This is not quite a dup - for C++ see and upvote the accepted answer here by #Zunino.
Basic code below but to see the full glorious elegance of the answer you are going to have to click on it.
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
int main() {
using namespace std;
string sentence = "Something in the way she moves...";
istringstream iss(sentence);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(cout, "\n"));
}
This hinges on the fact that by default, istream_iterator treats whitespace as its separator. The resulting tokens are written to cout on separate lines (per separator specified in constructor overload for ostream_iterator).
The easiest method is boost::split:
std::vector<std::string> words;
boost::split(words, your_string, boost::is_space());