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

  • C/C++
  • Thread starter Jamin2112
  • Start date
In summary: C++11 is the minimum version of the language that should be targeted.In summary, during an in-person interview, the interviewer asked for a function that replaces all As in a string with three As. The person was allowed to use the replace function but found it difficult to write the function using an iterator. They requested an elegant solution without using the boost library. Two potential solutions were provided, one using regular expressions and the other using standard C++11. The person also clarified that C++11 is now considered a legacy programming tool and C++14 is the current standard.
  • #1
Jamin2112
986
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
  • #2
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");
}
 
  • #3
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
 
  • #4
Not boost: standard C++11.

You didn't say it had to work with a legacy programming tools :smile:
 
  • #5
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]);
}
 
  • #6
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.
 

1. How do I replace all "A" characters with "***" in a C++ program?

To replace all "A" characters with "***" in a C++ program, you can use the replace function from the algorithm library. This function takes in three parameters: the start and end iterators of the string, and the character to be replaced. You can use a loop to iterate through the string and call the replace function to replace each "A" character with "***".

2. Can I replace only specific instances of "A" with "***" in a C++ program?

Yes, you can use the replace function to replace specific instances of "A" with "***" in a C++ program. You can specify the starting and ending index of the characters you want to replace in the string.

3. How do I replace "A" with "***" in a specific portion of a C++ string?

To replace "A" with "***" in a specific portion of a C++ string, you can use the replace function and specify the starting and ending index of the portion in the string. This will only replace "A" characters within that portion of the string.

4. Can I use a different character or string to replace "A" in a C++ program?

Yes, you can use any character or string to replace "A" in a C++ program. The replace function allows you to specify the replacement character or string as the third parameter.

5. Is there a more efficient way to replace "A" with "***" in a C++ program?

There are different approaches to replacing "A" with "***" in a C++ program, but using the replace function is a simple and efficient method. You can also use other string manipulation functions, but they may require more code and may not be as efficient as the replace function.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
4
Views
12K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top