-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcatenate_strings.pl
24 lines (20 loc) · 1.97 KB
/
concatenate_strings.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
%****************************************************************************************************
% *
% Predicate to Concatenate Two Strings *
% *
% The following Prolog code defines the predicate `concatenate_strings/3` to concatenate two *
% strings into one. *
% *
% - `concatenate_strings/3`: Entry point to concatenate two strings. *
% *
% Example usage: *
% ?- concatenate_strings("hello", "world", Result). *
% Result = "helloworld" *
% *
%****************************************************************************************************
% ----------------------------------------Predicate to concatenate two strings----------------------------------------
concatenate_strings(String1, String2, Result) :-
string_chars(String1, CharList1), % Convert first string to list of characters
string_chars(String2, CharList2), % Convert second string to list of characters
append(CharList1, CharList2, ResultList), % Concatenate the lists of characters
string_chars(Result, ResultList). % Convert the concatenated list back to a string