Replace a character c1 with c2 and c2 with c1 in a string S It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. Doubling the cube, field extensions and minimal polynoms. Learn more, C# Program to replace a special character from a String, C# program to replace all spaces in a string with %20. C++ Program to find and replace in a string C++ Server Side Programming Programming Suppose, we are given a string s. There is a range, from start to end where start and end are both integers. I refactored it as an extension method for Regex, which I can't show in a comment so will add as a somewhat redundant extra answer below. The alternative is to walk through the string making sure you don't consume the replacements in further replace operations. If you would like to change your settings or withdraw consent at any time, the link to do so is in our privacy policy accessible from our home page.. We and our partners use cookies to Store and/or access information on a device. Next, we used the If Statement to check the index value is not equal to -1. Use this function : char *replace(char *st, char *orig, char *repl) { String Be the first to rate this post. Next, it will search and replace all occurrences of a character inside a string.if(typeof ez_ad_units!='undefined'){ez_ad_units.push([[580,400],'tutorialgateway_org-medrectangle-4','ezslot_4',180,'0','0'])};__ez_fad_position('div-gpt-ad-tutorialgateway_org-medrectangle-4-0'); First we used For Loopto iterate each and every character in a String. char a,b,str[100]; How to replace table names with object types in a string? Replace Now copy original string to new string with replacement of word. The recommended solution is to use the StringBuilder class to efficiently replace the character at a specific index in a string in C#, as shown below: 2. I have created following extension methods to perform this operation: public static string Replace(this string str, Dictionary