Write a function replace(s, old, new) that replaces all occurrences of old with new in a string s: code example
Example 1: You will be provided a file path for input I, a file path for output O, a string S, and a string T. Read the contents of I, replacing each occurrence of S with T and write the resulting information to file O. You should replace O if it already exists.
# You will be provided a file path for input I, a file path for output O, a string S, and a string T.
# Read the contents of I, replacing each occurrence of S with T and write the resulting information to file O.
# You should replace O if it already exists.
file1 = open(I, 'r')
Icontent = file1.read()
editedData = Icontent.replace(S, T)
file2 = open(O, 'w')
file2.write(editedData)
file1.close()
file2.close()
Example 2: javascript create a function that counts the number of syllables a word has. each syllable is separated with a dash -.
function new_count(word) {
word = word.toLowerCase();
if(word.length <= 3) { return 1; }
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
word = word.replace(/^y/, '');
return word.match(/[aeiouy]{1,2}/g).length;
}
console.log(new_count('she'));
console.log(new_count('spain'))
console.log(new_count('softball'))
console.log(new_count('contagion'))