Challenge: String Manipulation Practice
replace.h
12// replaces n characters starting from start with str2 str.replace(start, n, str2)
Swipe to start coding
You are creating a content moderation system for user comments.
Your goal is to censor forbidden words by replacing them with "***"
.
The function censorComment
takes a comment
and a forbiddenWord
as string
.
- First, find the position of the forbidden word in the comment using
find
method. - Make sure the word is actually present in the comment by checking that the position is not
-1
before attempting any replacement. - Use
replace(start, n, str2)
to censor the word:- First parameter: the starting position of the forbidden word (
pos
). - Second parameter: the length of the forbidden word
forbiddenWord
. - Third parameter: the string to replace the forbidden word (
"***"
).
- First parameter: the starting position of the forbidden word (
- Return the modified comment.
Example
censorComment("This movie is stupid", "stupid")
→ "This movie is ***"
censorComment("I hate bugs in code", "hate")
→ "I *** bugs in code"
censorComment("What a nice day", "bad")
→ "What a nice day"
Løsning
solution.cpp
Takk for tilbakemeldingene dine!
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 4.35
Challenge: String Manipulation Practice
Sveip for å vise menyen
replace.h
12// replaces n characters starting from start with str2 str.replace(start, n, str2)
Swipe to start coding
You are creating a content moderation system for user comments.
Your goal is to censor forbidden words by replacing them with "***"
.
The function censorComment
takes a comment
and a forbiddenWord
as string
.
- First, find the position of the forbidden word in the comment using
find
method. - Make sure the word is actually present in the comment by checking that the position is not
-1
before attempting any replacement. - Use
replace(start, n, str2)
to censor the word:- First parameter: the starting position of the forbidden word (
pos
). - Second parameter: the length of the forbidden word
forbiddenWord
. - Third parameter: the string to replace the forbidden word (
"***"
).
- First parameter: the starting position of the forbidden word (
- Return the modified comment.
Example
censorComment("This movie is stupid", "stupid")
→ "This movie is ***"
censorComment("I hate bugs in code", "hate")
→ "I *** bugs in code"
censorComment("What a nice day", "bad")
→ "What a nice day"
Løsning
solution.cpp
Takk for tilbakemeldingene dine!
single