A popular utility for applications.
★★☆Roly Poly is a classic nursery rhyme for teaching young children opposites such as up and down, big and small, fast and slow. In the rhyme, the word, "up" can be replaced with "big", and the word, "down" replaced with "small" in the second verse.
Write a program that asks the user which word they want to replace in the rhyme: roly poly roly poly up up up roly poly roly poly down down down. The individual words can be stored in an array. All occurrences of that word are replaced with a word chosen by the user.
Note: do not use the Replace method if you are aware of it. You are writing the code that might have been used to make that method.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
replace that:x is the word to be replaced, y is the word to replace x with, and l is an array of words.x is replaced with the word y.output that:output subprogram.replace subprogram is called to replace the word in the array.output subprogram.roly
poly
roly
poly
up
up
up
roly
poly
roly
poly
down
down
down
Which word to replace? :
up
Replace with what? :
big
roly
poly
roly
poly
big
big
big
roly
poly
roly
poly
down
down
down
// Find and replace program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
// Function to replace x with y in list l
static string[] replace(string x, string y, string[] l)
{
---
for (int index = 0; index < l.Length; index++)
{
---
if (l[index] == x)
{
---
l[index] = y;
---
}
---
}
---
return l;
---
}
---
// Procedure to output the list, each item on a new line
static void output(string[] l)
{
---
foreach(string word in l) {
Console.WriteLine(word);
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
string[] rhyme = {"roly", "poly", "roly", "poly", "up", "up", "up", "roly", "poly", "roly", "poly", "down", "down", "down"};
output(rhyme);
Console.WriteLine("Which word to replace? :");
string word = Console.ReadLine();
Console.WriteLine("Replace with what? :");
string new_word = Console.ReadLine();
rhyme = replace(word, new_word, rhyme);
output(rhyme);
---
}
}