Welcome to W3Courses
 Like Us on Facebook

Replace

Share

Replace Function using Prolog Source Code

The following code replaces an element with a new element in the list

 CODE
 replace([],A,B,[]).
 replace([H|T],A,B,[B|Result]) :- H=A, replace(T,A,B,Result).
 replace([H|T],A,B,[H|Result]) :- replace(T,A,B,Result).


 RESULT
 ?- replace([a,b,a,c,a,d],a,w,R).
 R = [w, b, w, c, w, d]
 Yes

Share

Replace an Element from a List using Scheme Source Code

The following code replaces an element from a list

CODE
(define replace( lambda (x b y)
      (if (null? y )
             ()
      (if (eqv? (car y) x)
             (cons b (replace x b (cdr y)))
      (cons (car y) (replace x b (cdr y)))))))

Share

String Replace Function in C#

The following code will replace a character or a word with some other character or a word. This is accomplished by the replace function in C#.

using System;
using System.Text.RegularExpressions;

string strReplace;
strReplace = "This is a test string.";
strReplace = Regex.Replace(strReplace, " is", " was");

Share