Function modify a letter in a string Learn programming C#



Lesson:

Functions


Exercise:

Function modify a letter in a string


Objetive:

Create a function named "ChangeChar" to modify a letter in a certain position (0 based) of a string, replacing it with a different letter:

string sentence = "Tomato";
ChangeChar(ref sentence, 5, "a");


Code:

using System;
public class exercise110
{
    public static void ChangeChar(ref string text, int position, char letter)
    {
        text = text.Remove(position, 1);
        text = text.Insert(position, letter.ToString());
    }

    public static void Main()
    {
        string sentence = "Tomato";
        ChangeChar(ref sentence, 5, 'a');
    }
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.