'/n'

Is there anyway to replace a whole sentence or paragraph without replacing the '/n'? Right now if i replace a sentence with asterisks using the replace function it replaces the '/n' and moves the next sentence all on one line. Any suggestions?
Have you tried one less character?
Is there anyway to replace a whole sentence or paragraph without replacing the '/n'?


First, this doesn't make sense. C++ doesn't have any concept of sentence or paragraph, so being more specific might be good.

If you want to "replace a whole string without replacing part of it" you're not replacing a whole string. It might follow that replace, alone, isn't going to do the job. There are other tools one can use to manipulate or determine information about strings. Perhaps you can use one of them in tandem with replace (or in place of replace) to get the behavior you want.

well it may not make sense to you but pogradry seemed to understand the question well enough.
but to be more specific this is what i meant to replace a sentence of characters ended by a punctuation such as, '!','?','.' . If the replace function cant get the job done then i don't know what will. I want to replace each character in the sentence with asterisks. But when i replace all of the characters it replaces the '/n' as well making just a big line of asterisks. I want to know if there is a way to stop it from replacing '/n' and so far i think pogrady's idea works the best. If i need to be more clear. I will try my best. Thank you
What pogrady suggested is the best way to do this. Another way will be to replace everything and just append a '\n' character to the last letter.

string = "blah blah blah\n"
\*PRESTO transform*/
string = "***************"
string[string.length] = "\n"
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string str = "To be, or not to be, that is the question:\n"
                       "Whether 'tis Nobler in the mind to suffer\n"
                       "The Slings and Arrows of outrageous Fortune,\n"
                       "Or to take Arms against a Sea of troubles,\n"
                       "And by opposing end them: to die, to sleep\n"
                       "No more; and by a sleep, to say we end\n"
                       "The Heart-ache, and the thousand Natural shocks\n"
                       "That Flesh is heir to?\n" ;

    std::cout << str << '\n' ;

    // replace everything except punctuation and control characters
    for( char& c : str ) if( !( std::ispunct(c) || std::iscntrl(c) ) ) c = '*' ;
    std::cout << str << '\n' ;

    // replace everything except '\n'
    for( char& c : str ) if( c != '\n' ) c = '*' ;
    std::cout << str << '\n' ;
}
that is a way i haven't thought of JLBorges. Thank you i will try that.
My five cents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
	std::string s( "To be, or not to be, that is the question:\n" );

	std::string t[] = { "To eat", "or not to eat", "that is not the question" };

	std::cout << s << std::endl;

	for ( std::string::size_type i = 0, pos = 0; i < sizeof( t ) / sizeof( *t ); i++ )
	{
		while ( s[pos] == ' ' || s[pos] == '\t' ) ++pos;
		auto it = std::find_if( std::next( s.begin(), pos ), s.end(),
			[]( char c ) { return ( ispunct( c ) ); } );

		if ( it == s.end() ) break;

		s.replace( pos, std::string::size_type( distance( s.begin(), it ) ) - pos,
				       t[i] );

		pos += t[i].size() + 1;
	}

	std::cout << s << std::endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.