Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get substring in C++ and add character in between string

I have a string Hello foo how are you.

I want to change it to Hello\r\nfoo how are you.

I wanted to know the way to get the substring Hello add \r\n in place of space and add all other strings as they are. This is to show the multiline.

EDIT:

We don't know the length of first substring. We would not know how long the first substring will be.

Thanks.

like image 820
rpmish99 Avatar asked Sep 20 '25 04:09

rpmish99


1 Answers

Not a complete answer, because this looks like homework, but other approaches you could use include std::find() in <algorithm> and strchr() in <string.h>. If you need to search for any whitespace and not just the ' ' character, you might use std::find_first_of() or strcspn().

In the future, I’d check out the documentation for: the member functions of std::basic_string, the utility functions in <string>, the functions in <algorithm>, and the functions in <string.h>, as those will generally be the tools you have to work with.

like image 127
Davislor Avatar answered Sep 22 '25 18:09

Davislor