C/C++ "Replace all As with three ***" (C++)

  • Thread starter Thread starter Jamin2112
  • Start date Start date
AI Thread Summary
The discussion centers around a coding challenge where an interviewer requested a function to replace all instances of 'A' or 'a' in a string with three 'A's. The original poster struggled with using the replace function due to unclear documentation on string re-indexing. They shared a solution using C++11's regex capabilities, which effectively replaces 'A' and 'a' with three occurrences. Other participants contributed alternative approaches, including a manual method that iterates through the string and inserts additional characters. The conversation also touched on the classification of C++11 as "legacy programming tools," with a note that C++14 is the current standard. The emphasis remained on finding elegant and efficient solutions without using the Boost library.
Jamin2112
Messages
973
Reaction score
12
During my in-person interview yesterday, the interviewer asked me to write a function that takes a string and replaces all As with three As

Ex. "Abraham"--->"AAAbraaahaaam"

I did it, but it took me a while to get it right using the replace function (http://www.cplusplus.com/reference/string/string/replace/), because the documentation (which I was allowed to use) doesn't say how the string is re-indexed after using the function. I found it difficult to write the function by iterating through it with an iterator.

Anyways, I was wondering whether someone here could show me their most elegant solution, just so I can see what I could've improved on.
 
Technology news on Phys.org
Not tested, but no iterators were harmed in making this function:
Code:
#include <string>
#include <regex>

string tripleA(string s) 
{
return  std::regex_replace(s, std::regex("([Aa])"), "$1$1$1");
}
 
AlephZero said:
Not tested, but no iterators were harmed in making this function:
Code:
#include <string>
#include <regex>

string tripleA(string s) 
{
return  std::regex_replace(s, std::regex("([Aa])"), "$1$1$1");
}

No boost library, please
 
Not boost: standard C++11.

You didn't say it had to work with a legacy programming tools :smile:
 
Old school:

Code:
void tripleA(std::string &s)
{
    for (auto i = s.size(); i > 0; --i)
        if (tolower(s[i - 1] == 'a')
            s.insert(i, 2, s[i - 1]);
}
 
AlephZero said:
Not boost: standard C++11.

You didn't say it had to work with a legacy programming tools :smile:
C++11 has fallen into the status of "legacy programming tools". As of August 18, 2014, the current standard is C++14.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top