7 kyu

Replace every nth

1,026 of 4,446user5036852

Description:

Task

Write a method, that replaces every nth char oldValue with char newValue.

Inputs

  • text: the string to modify
  • n: change the target letter every nth occurrencies
  • old_value (or similar): the targetted character
  • new_value (or similar): the character to use as replacement

Note for untyped languages: all inputs are always valid and of their expected type.

Rules

  • Your method has to be case sensitive!
  • If n is 0 or negative or if it is larger than the count of the oldValue, return the original text without a change.

Example:

n:         2
old_value: 'a'
new_value: 'o'
"Vader said: No, I am your father!"
  1     2          3        4       -> 2nd and 4th occurence are replaced
"Vader soid: No, I am your fother!"

As you can see in the example: The first changed is the 2nd 'a'. So the start is always at the nth suitable char and not at the first!

Algorithms
Strings

Similar Kata:

Stats:

CreatedOct 11, 2016
PublishedOct 11, 2016
Warriors Trained10106
Total Skips950
Total Code Submissions20428
Total Times Completed4446
JavaScript Completions1026
C# Completions235
Java Completions953
F# Completions36
C++ Completions875
TypeScript Completions146
Python Completions875
C Completions375
Ruby Completions113
CoffeeScript Completions14
Clojure Completions20
COBOL Completions3
Total Stars110
% of votes with a positive feedback rating91% of 863
Total "Very Satisfied" Votes722
Total "Somewhat Satisfied" Votes125
Total "Not Satisfied" Votes16
Total Rank Assessments12
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • user5036852 Avatar
  • jhoffner Avatar
  • kevinthomas Avatar
  • suic Avatar
  • Dentzil Avatar
  • lilsweetcaligula Avatar
  • Blind4Basics Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • akar-0 Avatar
Ad