using System;
using System.Text;
public class Program
{
public static void Main()
String s = "thaaaa"; //I want to replace h with a and vice versa
s = ReplaceChar(s, 'a', 'd', 5);
Console.WriteLine(s);
}
public static string ReplaceChar(string s, char c, char d, uint index)
if(index > s.Length)
return s;
int count = 0;
StringBuilder b = new StringBuilder();
foreach (char a in s.ToCharArray())
if (a == c)
count++;
if (index == count)
b.Append(d);
continue;
b.Append(a);
return b.ToString();