- Course
- Functions B
- Function to modify a character of a text
Function to modify a character of a text
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a function called ChangeLetter that receives as a parameter a text, a position (based on 0) and a letter requested from the user. You must modify the character of the text and display the new text on the screen.
Input
Card
3
Output
Car
Solution
using System;
class FunctionChangeLetter
{
public static void Main(string[] args)
{
string text = Console.ReadLine();
int position = Convert.ToInt32(Console.ReadLine());
char letter = Convert.ToChar(Console.ReadLine());
ChangeLetter(text, position, letter);
}
public static string ChangeLetter(string text, int position, char letter)
{
text = text.Remove(position, 1);
text = text.Insert(position, letter.ToString());
return text;
}
}