How to replace all "pi" from a string by "3.14"? Example: INPUT = "xpix" ___ OUTPUT = "x3.14x" for a string, not character array.
This doesn't work:
#include<iostream>
using namespace std;
void replacePi(string str)
{
if(str.size() <=1)
return ;
replacePi(str.substr(1));
int l = str.length();
if(str[0]=='p' && str[1]=='i')
{
for(int i=l;i>1;i--)
str[i+2] = str[i];
str[0] = '3';
str[1] = '.';
str[2] = '1';
str[3] = '4';
}
}
int main()
{
string s;
cin>>s;
replacePi(s);
cout << s << endl;
}
There is a ready to use function in the C++ lib. It is called: std::regex_replace. You can read the documentation in the CPP Reference here.
Since it uses regexes it is very powerful. The disadvantage is that it may be a little bit too slow during runtime for some uses case. But for your example, this does not matter.
So, a common C++ solution would be:
#include <iostream>
#include <string>
#include <regex>
int main() {
// The test string
std::string input{ "Pi is a magical number. Pi is used in many places. Go for Pi" };
// Use simply the replace function
std::string output = std::regex_replace(input, std::regex("Pi"), "3.14");
// Show the output
std::cout << output << "\n";
}
But my guess is that you are learning C++ and the teacher gave you a task and expects a solution without using elements from the std C++ library. So, a hands on solution.
This can be implemented best with a temporary string. You check character by character from the original string. If the characters do not belong to Pi, then copy them as is to new new string. Else, copy 3.14 to the new string.
At the end, overwrite the original string with the temp string.
Example:
#include <iostream>
#include <string>
using namespace std;
void replacePi(string& str) {
// Our temporay
string temp = "";
// Sanity check
if (str.length() > 1) {
// Iterate over all chararcters in the source string
for (size_t i = 0; i < str.length() - 1; ++i) {
// Check for Pi in source string
if (str[i] == 'P' and str[i + 1] == 'i') {
// Add replacement string to temp
temp += "3.14";
// We consumed two characters, P and i, so increase index one more time
++i;
}
else {
// Take over normal character
temp += str[i];
}
}
str = temp;
}
}
// Test code
int main() {
// The test string
std::string str{ "Pi is a magical number. Pi is used in many places. Go for Pi" };
// Do the replacement
replacePi(str);
// Show result
std::cout << str << '\n';
}
What you need is string::find and string::replace. Here is an example
size_t replace_all(std::string& str, std::string from, std::string to)
{
size_t count = 0;
std::string::size_type pos;
while((pos=str.find(from)) != str.npos)
{
str.replace(pos, from.length(), to);
count++;
}
return count;
}
void replacePi(std::string& str)
{
replace_all(str, "pi", "3.14");
}
I was writing a code that would substitute some random 17 character strings into a single alphabet, and I can't find a way. Basically, what I'm trying to do is this:
char strings[] = {
"L-nIbhm5<z:92~+,x",
"9bC5f0q#qA(RKZ>|r",
"9bC5f0q#qA(RKZ>|r",
"k=5,ln(08IAl(gGAK",
"|N,8]dGu)'^MaYpu[",
"!&,Y*nz8C*,J}{+d]",
"Us9%^%?n5!~e##*+#",
"zF8,1KV#¥]$k?|9R#",
"0B4>=nioEjp>4rhgi",
}
char alphabet[]{
"a","b","c","d","e","f","g","h","i",
}
replace(std::string str){
/**get str and then see the index of the corresponding string in strings[], and replace the string with alphabet[index number], while deleting the original string part that was replaced**/
int main(){
cin >> std::string replace;
replace(replace);
example input: L-nIbhm5<z:92~+,x9bC5f0q#qA(RKZ>|r9bC5f0q#qA(RKZ>|r
expected output: abc
EDIT:
New Code
Changes from the original code
It also has a bigger array than the simplified version(previous code). It displays the structure of the full program.(where the strings are routed to and why)
Basically What it's doing
getting input from user, put it in the input variable, input goes through algorithm() function untouched, and then goes to the replace function and is replaced. It then the replaced string gets returned back through the original route to the main function, where it is displayed.
I've kept the arrays a string type because the const char* gave me a segmentation error.
std::string Subs[53]=
{
"LQlMv]G5^^1kcm?fk",
"7W^S;/vB(6%I|w[fl",
"<w7>4f//Z55ZxK'z.",
"_W5g(lu<pTu3^_A7n",
"OfLm%8:EF}0V1?BSS",
"|+E6t,AZ~XewXP17T",
"L-nIbhm5<z:92~+,x",
"L-nIbhm5<z:92~+,x",
"9bC5f0q#qA(RKZ>|r",
"9bC5f0q#qA(RKZ>|r",
"k=5,ln(08IAl(gGAK",
"|N,8]dGu)'^MaYpu[",
"!&,Y*nz8C*,J}{+d]",
"Us9%^%?n5!~e##*+#",
"zF8,1KV#¥]$k?|9R#",
"0B4>=nioEjp>4rhgi",
"EG#0[W9.N4i~E<f3x",
"(0Pwkk&IPchJHs.7A",
"7XgmQ6fW<|J+NY[m0",
".g4CwX/DU!!~!zbtZ",
"+_U'qn_/9Fo|gT/!n",
"=0s(mYh&F%y=MBS5(",
"cg71(}bo+Q5P8F[T6",
"lc|a\%5.9pOpooU+QR",
"E_(3A:o+.]qL3MYA6",
"H#O'X_RiVS#8l0bKD",
"Y1gbGD`~8d>HSWN35",
"LQlMv]G5^^1kcm?fk",
"T4}gI;`BFVfhw=-sf",
"6BHMA0IRix]/=(jht",
"yS$=#Jdpp?P2k6SMQ",
"t1~|kkh+>4d>}OQ`a",
"2Y-\\CU\"944yBluWD5",
"'M\\ZbIX5{`Xd;qi!o",
"?N+RtVqj_r(C5##0\"",
"2;*Livh?V$X/8z#Md",
")IN|7FOs2l-mAM[d#",
"(~f268J},xXrK'Rp'",
"&r/qf9fFHnzV!RzH/",
"}naDRH4p$NI2a).t,",
"{8DM+7!.Mge|~fnO|",
")r[#nI0YDH>6cE38p",
"(0Pwkk&IPchJHs.7A",
")r[#nI0YDH>6cE38p",
"8M-=cQFQ,pPo7eu=p",
"0PHw=/|(tZ1}FHm/'",
"[su`'0Oybc.\"-/W5)",
"1uHl[IC7Sr#NUJV;I",
"8z8%,jK0CDOkJz8I?",
"3Ao2yXDN%YzpE&Suy",
"zNs`7E'e/$i8VqaUL",
"bzHmA^K2>7`UZ?!AO",
};
std::string Alphabet[53] =
{
" ","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","r","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
};
std::string replace(std::string rep) {
int len = sizeof(Subs)/sizeof(Subs[0]);
std::stringstream ss1;
for(int i = 0; i < len; i++) {
if (rep.find(Subs[i]) != std::string::npos) {
ss1 << Subs[i];
}
}
std::string input = ss1.str();
return input;
}
std::string algorithm(std::string input)
{
//some other algorithms come here(not relative to this question)
input = replace(input);
return input;
}
int main(void){
int ed;
std::cin >> ed;
if(ed == 1){
//different function(not relative to the question)
}
else if(ed == 0){
std::string input;
std::cin >> input;
input = algorithm(input);
std::cout << input << std::endl;
}
else{
std::cout << "1 or 0" << std::endl;
main();
}
return 0;
}
example input: L-nIbhm5<z:92~+,x9bC5f0q#qA(RKZ>|r9bC5f0q#qA(RKZ>|r
expected output: abc
actual output: L-nIbhm5<z:92~+,xL-nIbhm5<z:92~+,x9bC5f0q#qA(RKZ>|r9bC5f0q#qA(RKZ>|r
Sorry it's become long.
There are few mistakes in above code :
char array initialization is not correct.
method body for main and replace method is not closed.
Currently by default return type of replace method is int.
There is string#find method which can be helpful here.
I have tried to make those fixes and here is updated code in C++17 :
#include <iostream>
#include <sstream>
using namespace std;
const char *strings[9] = {
"L-nIbhm5<z:92~+,x",
"9bC5f0q#qA(RKZ>|r",
"9bC5f0q#qA(RKZ>|r",
"k=5,ln(08IAl(gGAK",
"|N,8]dGu)'^MaYpu[",
"!&,Y*nz8C*,J}{+d]",
"Us9%^%?n5!~e##*+#",
"zF8,1KV#¥]$k?|9R#",
"0B4>=nioEjp>4rhgi"
};
const char *alphabet[9] = {
"a","b","c","d","e","f","g","h","i"
};
void replace(std::string rep) {
int len = sizeof(strings)/sizeof(strings[0]);
std::stringstream ss1;
for(int i = 0; i < len; i++) {
if (rep.find(strings[i]) != std::string::npos) {
ss1 << alphabet[i];
}
}
std::cout << ss1.str();
}
int main(){
std::string rep;
cin >> rep;
replace(rep);
}
For reference : https://onlinegdb.com/Bd9DXSPAa
Note - Above code is just for reference, please make sure to add all test cases handling.
I made a c++17 version for your code.
Replacing 'c' style arrays and pointers with C++ style containers, iterators.
And using std::string::replace function. Use the standardlibrary if you can,
its tested and well documented.
#include <algorithm>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
// std::vector/std::array instead of 'c' style arrays.
// allows us to us range based for loops later.
std::vector<std::string> strings =
{
"L-nIbhm5<z:92~+,x",
"9bC5f0q#qA(RKZ>|r",
"k=5,ln(08IAl(gGAK",
"|N,8]dGu)'^MaYpu[",
"!&,Y*nz8C*,J}{+d]",
"Us9%^%?n5!~e##*+#",
//"zF8,1KV#¥]$k?|9R#", // <<== I commented out this line, ¥ is not a valid charcter in my environment
"0B4>=nioEjp>4rhgi"
};
// a string is already an array of characters.
std::string alphabet{ "abcdefghijkl" };
std::string replace_with_alphabet(const std::string& input)
{
std::string retval{ input };
std::size_t index{ 0 };
// range based for, it will keep the order of the vector.
for (const auto& str : strings)
{
// look if you can find any of the predefined strings
// in the input strings.
const size_t pos = retval.find(str, 0);
// if found
if (pos != std::string::npos)
{
// get the next character from the alphabet
std::string replacement{ alphabet[index++] };
// use std::string::replace for replacing the substring
const size_t len = str.length();
retval.replace(pos, len, replacement, 0);
}
}
return retval;
};
/**get str and then see the index of the corresponding string in strings[], and replace the string with alphabet[index number], while deleting the original string part that was replaced**/
int main()
{
auto output = replace_with_alphabet("L-nIbhm5<z:92~+,x9bC5f0q#qA(RKZ>|rk=5,ln(08IAl(gGAK");
std::cout << output << std::endl;
}
How i could convert this javascript code to c++
key = {"|1|":"A","|2|":"B","|4|":"C","|3|":"D"}
y = "|1||2||3||4|"
for (i in key)
y = y.replace(i,key[i])
console.log(y)
output: ABDC
I got the "decoding key" in a std::string like:
std::string key = "{\"|1|\":\"A\",\"|2|\":\"B\",\"|3|\":\"D\",\"|4|\":\"C\"}";
Based on the properties of this key do a string replace in my_string.
Example:
std::string key = "{\"|1|\":\"A\",\"|2|\":\"B\",\"|3|\":\"D\",\"|4|\":\"C\"}";
std::string my_string = "|1||2||3||4|";
// . . .
Replace in my_string |1| to A, |2| to B, |3| to D, |4| to C
|1| to A, |2| to B ... etc comes from the std::string key
Then my_string now is ABDC.
Do i need to convert the std:: string key to another data type? I mean something similar to an object like on javascript, im not familiar with c++.
Equivalents of java dictionnary is map or unordered_map
As for your json question, you need a library to parse json, I recommend the excellent one from Professor Lemire (github lemire)
I just wrote you a simple json parser for you. Note that you will need to compile with c++1z or you will get the warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
g++ -std=c++1z test.cpp && ./a.out
#include <unordered_map>
#include <iostream>
#include <sstream>
using namespace std;
void jsonDecode(unordered_map <string, char> &m, string s) {
constexpr char delimiter = '"';
for (int i = 0; i < s.size(); ) {
if (s[i++] == delimiter) {
stringstream ss;
while (s[i] != delimiter)
ss << s[i++];
string key = ss.str();
++i;
while (s[i] != delimiter) i++;
char value = s[++i];
m[key] = value;
i+=2;
}
}
}
int main(void) {
unordered_map <string, char> m;
string s = R"({"|1|":"A","|2|":"B","|3|":"D","|4|":"C"})";
jsonDecode(m, s);
string y = "|1||2||3||4|";
for (auto &[k,v] : m)
y.replace(y.find(k), k.size(), string(1, v));
cout << y << endl;
return 0;
}
output:
ABDC
You can read about replace method here
Here is a direct "translation" to C++:
#include<iostream>
#include<string>
#include<unordered_map>
int main()
{
std::unordered_map<std::string, std::string> dictionary = { {"|1|","A"} , {"|2|", "B"}, {"|4|", "C"}, {"|3|", "D"} };
std::string y = "|1||2||3||4|";
size_t pos;
for (auto& a : dictionary) {
while((pos = y.find(a.first)) != std::string::npos)
y.replace(pos, a.first.length(), a.second);
}
std::cout << y << std::endl;
return 0;
}
I'm using std::regex_replace to modify the string. I need both to restrict substitutions made and to get number of them done.
I used following code:
std::wregex rx(pattern);
size_t n = 0; // one match
size_t ns = 3;
wstring result = src;
wstring old; // old value
bool replaced = false;
do {
old = result;
result = std::regex_replace(result, rx, replace, std::regex_constants::format_first_only);
replaced = result != old;
if (replaced)
n++;
} while (replaced && n < ns);
It works fine, I can both restrict substitutuins amount and get their number. But this code analyzes the string from its start, as a result, if I have following values:
"banana" for src, "(an)" for pattern and "$1-" for replace
it produces following output: ban---ana instead of ban-an-a. Obviously, this is because std::regex_replace analyzes string from start. A solution might be to use iterators to define first character to analyze, but in this case I need to get iterator that points to characters after those that were replcased, but how can I get it?
This turned out to be trickier than I thought. I didn't find any of the std::regex_replace() functions very helpful here.
I decided to go with a straight std::wsregex_iterator solution based on the algorithm suggested in the implementation descriptions here:
http://en.cppreference.com/w/cpp/regex/regex_replace
This is what I came up with:
#include <regex>
#include <string>
#include <iterator>
#include <iostream>
int main()
{
std::size_t ns = 3;
std::wstring text = L"banana";
std::wstring pattern = L"(an)";
std::wstring replace = L"$1-";
std::wstring result;
std::wregex rx(pattern);
auto iter = std::wsregex_iterator(text.begin(), text.end(), rx);
auto stop = std::wsregex_iterator();
auto last_iter = iter;
auto out = std::back_inserter(result);
for(std::size_t n = ns; n-- && iter != stop; ++iter)
{
out = std::copy(iter->prefix().first, iter->prefix().second, out);
out = iter->format(out, replace);
last_iter = iter;
}
out = std::copy(last_iter->suffix().first, last_iter->suffix().second, out);
std::wcout << " text: " << text << '\n';
std::wcout << "result: " << result << '\n';
}
Output:
text: banana
result: ban-an-a
You can set up a counter and use a callback. This will only replace the maximum you set.
(Untested)
static int REPL_count = 0;
static int REPL_max = 0;
static string REPL_replace = "";
string REPLcallback( const wsmatch m )
{
// Return formatted match if under the max count
if ( REPL_count < REPL_max )
{
++REPL_count;
return m.format( REPL_replace );
}
// Exceeded count, just return match without formatting
return string(m[0].first, m[0].second);
}
int ReplaceText(
string& strIn,
string& strOut,
wregex Regex,
string& strReplacement,
int max )
{
REPL_count = 0;
REPL_max = max;
REPL_replace = strReplacement;
strOut = regex_replace( strIn, Regex, REPLcallback );
return REPL_count;
}
I am trying to spit a string in C++ with the following way:
#include <bitset>
#include <iostream>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/timer.hpp>
using namespace std;
size_t const N = 10000000;
typedef string::const_iterator iter;
typedef boost::iterator_range<iter> string_view;
template<typename C>
void test_custom(string const& s, char const* d, C& ret)
{
C output;
bitset<255> delims;
while (*d)
{
unsigned char code = *d++;
delims[code] = true;
}
typedef string::const_iterator iter;
iter beg;
bool in_token = false;
bool go = false;
for (string::const_iterator it = s.begin(), end = s.end(); it != end; ++it)
{
if (delims[*it])
{
if (in_token)
{
output.push_back(typename C::value_type(beg, it));
in_token = false;
}
}
else if (!in_token)
{
beg = it;
in_token = true;
}
else
{
if (!go)
{
cout << typename C::value_type(beg, it);
//outputs the first character
go = true;
}
}
}
if (in_token)
output.push_back(typename C::value_type(beg, s.end()));
output.swap(ret);
}
vector<string_view> split_string(string in, const char* delim = " ")
{
vector<string_view> vsv;
test_custom(in, delim, vsv);
return vsv;
}
int split()
{
string text = "123 456";
vector<string_view> vsv = split_string(text);
for (int i = 0; i < vsv.size(); i++)
cout << endl << vsv.at(i) << "|" << endl;
return 0;
}
The problem here is the fact that the first character is erased for one reason... The returned string are ' 23' and '456' but I want them to be '123' and '456'
So, the first character is ' ' and not '1'
I'm not familiar with boost::iterator_range, but it sure sounds like a pair of iterators.
If so, then in this code:
vector<string_view> split_string(string in, const char* delim = " ")
{
vector<string_view> vsv;
test_custom(in, delim, vsv);
return vsv;
}
you're returning iterators referring to a local string called in, that has ceased to exist when the function returns.
That's Undefined Behavior.
One fix would be to pass that string by reference.
By the way, one inefficient but simple and safe way to split a string on whitespace is to use a istringstream:
istringstream stream( source_string );
string word;
while( stream >> word ) { cout << word; }
Disclaimer: code untouched by compiler's hands.
Since you're already using Boost, you could use this (the "simple" way :P):
#include <boost/algorithm/string.hpp>
std::string text = "this is sample string";
std::vector<std::string> tokens;
boost::split(tokens, text, boost::is_any_of("\t "));
Or use whatever separator(s) you want to use as third argument.