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

  • Context: C/C++ 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a programming challenge in C++, specifically the task of writing a function that replaces all occurrences of the letter 'A' (or 'a') in a string with three 'A's. Participants share their approaches, code snippets, and experiences related to implementing this functionality.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their experience during an interview where they were asked to implement the function, noting difficulties with the string re-indexing when using the replace function.
  • Another participant proposes a solution using the C++ regex library, suggesting that it elegantly replaces 'A's with three 'A's without using iterators.
  • A similar code snippet is repeated by another participant, emphasizing the use of regex and explicitly stating that the Boost library should not be used.
  • One participant shares an "old school" approach using a loop and string insertion, indicating a preference for a more manual method of handling the string modification.
  • There is a discussion about the use of C++11 versus legacy programming tools, with some participants humorously debating the status of C++ standards.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to implement the function, with no consensus on the best approach. Some prefer modern techniques using regex, while others advocate for traditional methods.

Contextual Notes

Some solutions may depend on specific C++ standards, and there are unresolved issues regarding the handling of string indexing and the implications of using different libraries.

Who May Find This Useful

Programmers interested in string manipulation in C++, particularly those preparing for technical interviews or exploring different coding styles and standards.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
13K
Replies
81
Views
8K
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K