Print Friendly and PDF
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');
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');


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();
        }
    }
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: