Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to compare characters, strings and also reverse string

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 8550

Prolog program to compare characters, strings and also reverse string.

Code for Prolog program to compare characters, strings and also reverse string in Artificial Intelligence

domains
    strlist = string*
predicates
    start
    createlist(integer,strlist,strlist,strlist)
    strcmp(string,string)
    charcmp(char,string)
    reverse(strlist,strlist,strlist)
    
goal    
    clearwindow,
    start.

clauses
    start:-
        createlist(3,[],Newlist,List1),
        reverse(List1,[],List2),
        List2 = [Str1 | Tail],
        Tail = [Str2 | Tail1],
        Tail1=[Str3 | Str4],
        write("cmp string1 and string2"),nl,
        strcmp(Str1,Str2),
        write("cmp string1 and string3"),nl,
        strcmp(Str1,Str3),
        write("cmp string2 and string3"),nl,
        strcmp(Str2,Str3).
        
    createlist(Num,Oldlist,Newlist,List1):-
        Num > 0,
        write("Enter any string ="),
        readln(Str),
        Newlist = [Str | Oldlist],
        NN = Num - 1,
        createlist(NN,Newlist,List2,List1).
        
    createlist(_,Oldlist,_,List1):-
        List1 = Oldlist.    
    

    strcmp(Str1,Str2):-
        frontchar(Str1,Ch1,Rest1),
        charcmp(Ch1,Str2),
        Rest1<>"",
        strcmp(Rest1,Str2).
    
    strcmp(Str1,Str2).
    
    charcmp(Ch1,Str2):-
    
        frontchar(Str2,Ch2,Rest2),
        Ch1<>Ch2,
        Rest2<>"",
        charcmp(Ch1,Rest2).
    
    charcmp(Ch1,Str2):-
        frontchar(Str2,Ch2,Rest2),
        Ch1=Ch2,
        write("char=",Ch2),nl.

    charcmp(Ch1,Str2).
    reverse([],Inputlist,Inputlist).
    reverse([Head |Tail],List1,List2):-
        reverse(Tail,[Head | List1],List2).
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to compare characters, strings and also reverse string is from India.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!