Insert character in a string

closed account (GywfSL3A)
Hi,

I would like to insert some character after each number of my string
I found how to insert before each character, but what I want is :

string str = "12 + 5"

insert ^ after each number

Output = "12^ + 5^"

Thank you for your help
closed account (Dy7SLyTq)
http://www.cplusplus.com/reference/string/string/insert/
closed account (GywfSL3A)
Sorry, but I don't find what I want.
Last edited on
closed account (GywfSL3A)
I searched for like 1h30 and I didn't found a solution. All the topic and documentation are about inserting something at a specificate position. I want to add my character after every number. I know it's like "do it for me" and I'm sorry for that, but if you can show me the way, it would be appreciate
Last edited on
closed account (Dy7SLyTq)
i would just write a function to test if it is a digit and then if it is do a while loop to find the next character that isnt a digit and insert the ^ there
This is a very elementary problem, and I understand that you are struggling to go forward. Just giving you the answer, though, won't help.

However, I can give you a significant hint.

Don't insert. Copy.

For each character in your input string, copy it to your output string (appending it).

Once you can simply copy the string, try to figure out when you should append '^'s to your output string too.

Good luck!
closed account (GywfSL3A)
Thank you for your help and for the good start
For now, I have that.

It say "Digit" if he found digit and "No digit" If he don't find one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#include <iostream>
#include <string>
#include <algorithm>
 
using namespace std;
 
int main() {
  string str;
  getline (cin, str);
  
  if (find_if(str.begin(), str.end(), ::isdigit) != str.end()) {
    
    cout << "Digit";
  }
  else {
    
    cout << "No digit";
  }
 
}
That isn't copying, that's searching.
Topic archived. No new replies allowed.