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 = "potato";
ChangeChar(ref sentence, 5, 'a');
string sentence = "potato";
ChangeChar(ref sentence, 5, 'a');
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChangeChar
{
class Program
{
public static void ChangeCh( ref string text, int position, char letter)
{
text = text.Remove(position, 1);
text = text.Insert(position, letter.ToString());
}
static void Main(string[] args)
{
string sentence = "Tomato";
ChangeCh(ref sentence, 5, 'a');
Console.WriteLine(sentence);
Console.ReadLine();
}
}
}
Post A Comment:
0 comments: