Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to read a list of strings , integer and display all the strings with the closest length of the integer entered should go to a new list

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

write a complete prolog program to read a list of strings. Then
user is allowed to enter an integer All the strings with the
closest length of the integer entered should go to a new list.

Code for Prolog program to read a list of strings , integer and display all the strings with the closest length of the integer entered should go to a new list in Artificial Intelligence

Domains
    stringlist = string*
Predicates
    startup
    reading(stringlist)
    findClosestString(integer,stringlist,stringlist,stringlist)
Clauses
    startup:-
        write("\nEnter Any String = "),
        readln(Str),
        OldList = [Str],
        reading(OldList).

    reading(OldList):-
        write("\nDo You Want To Enter Another String (y/n) ? : "),
        readchar(Ans),
        write(Ans),nl,
        Ans = 'y',
        write("\nEnter Any String = "),
        readln(Str),
        Newlist = [Str|OldList],nl,
        reading(Newlist).
        
    reading(OldList):-
        write("\nEnter ANy Positive Number = "),
        readint(Number),
        findClosestString(Number,OldList,[],Newlist).
        
    findClosestString(_,[],List,_):-
        write(List).
        
    findClosestString(Number,Oldlist,List,Newlist):-
        Oldlist = [Head | Tail],
        str_len(Head,Length),
        Number - 1 <= Length,
        Number + 1 >= Length,
        Newlist = [Head | List],
        findClosestString(Number,Tail,Newlist,List2).
        
    findClosestString(Number,Oldlist,List,Newlist):-
        Oldlist = [Head | Tail],
        Newlist = List,
        findClosestString(Number,Tail,Newlist,List2).

Goal
    clearwindow,
    makewindow(1,2,3,"String Operations",0,0,50,80),
    startup.
                
                        
Output :-->

+-------------------------------String Operations------------------------------+
¦                                                                              ¦
¦Enter Any String = Mayank                                                     ¦
¦                                                                              ¦
¦Do You Want To Enter Another String (y/n) ? : y                               ¦
¦                                                                              ¦
¦Enter Any String = raj                                                        ¦
¦                                                                              ¦
¦                                                                              ¦
¦Do You Want To Enter Another String (y/n) ? : y                               ¦
¦                                                                              ¦
¦Enter Any String = rahul                                                      ¦
¦                                                                              ¦
¦                                                                              ¦
¦Do You Want To Enter Another String (y/n) ? : y                               ¦
¦                                                                              ¦
¦Enter Any String = kesha                                                      ¦
¦                                                                              ¦
¦                                                                              ¦
¦Do You Want To Enter Another String (y/n) ? : y                               ¦
¦                                                                              ¦
¦Enter Any String = shwetambari                                                ¦
¦                                                                              ¦
¦                                                                              ¦
¦Do You Want To Enter Another String (y/n) ? : y                               ¦
¦                                                                              ¦
¦Enter Any String = anukul                                                     ¦
¦                                                                              ¦
¦                                                                              ¦
¦Do You Want To Enter Another String (y/n) ? : n                               ¦
¦                                                                              ¦
¦Enter ANy Positive Number = 5                                                 ¦
¦["Mayank","rahul","kesha","anukul"]                                           ¦
¦Press the SPACE bar                                                           ¦
¦                                                                              ¦
+------------------------------------------------------------------------------+

  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to read a list of strings , integer and display all the strings with the closest length of the integer entered should go to a new list 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!