How to get the first letter of each word enter by user

╄→尐↘猪︶ㄣ 提交于 2021-01-29 17:21:09

问题


Here are the details of my code

name:-read(X),write('Name : '),write(X),nameCode(X).
nameCode(X):-nl, write('Name Code : ').

I would like to take the first 3 letters from each word and display it . What should be added to my code? Furthermore, the result i get only allows me to enter a single name from user. When i attempt to enter several names(peter jane mary) in the query, it display a syntax message as below

| ?- name.
|: peter jane mary.
* Syntax Error

Below are the results of what i want to print

Name : peter jane mary
Name Code : PJM

回答1:


1- First the user enters his/her first/middle/last name.

2- It is read.

3- string_chars breaks the string into characters : peter will become p,e,t,e,r

4- getFirstLetter Predicate extracts the first element from the list: from peter we get p.

5- upcase_atom convert lowercase letters to uppercase: p will become P.

6- display the answer using write.

 k:- 
  write('Enter First name: '),nl, 
  read(FName),nl, 
  string_chars(FName,N1),
  getFirstLetter(N1,L1),
  upcase_atom(L1,Str1),
  
    
  write('Enter Middle name: '),nl, 
  read(MName),nl, 
  string_chars(MName,N2),
  getFirstLetter(N2,L2),
  upcase_atom(L2,Str2),
  
    
  write('Enter Last name: '),nl, 
  read(LName),nl, 
  string_chars(LName,N3),
  getFirstLetter(N3,L3),
  upcase_atom(L3,Str3),
  write(Str1),write(' '),write(Str2),write(' '),write(Str3).  

 getFirstLetter([H|_],H).

Example:

?-k.

Enter First name:
peter

Enter Middle name:
jane

Enter Last name:
mary

P J M

___


A more challenging task: Remove the first letter from the name.

1- First the user enters his/her first/middle/last name.

2- It is read.

3- string_chars breaks the string into characters : peter will become p,e,t,e,r

4- removeFirstLetter predicate removes the first letter: p,e,t,e,r will become e,t,e,r

5- charstring predicate will convert e,t,e,r to "e","t","e","r", we do this using term_string (this is important for the next step)

6- atomic_list_concat joins all the separate charaters together: "e","t","e","r" will become eter

 k:- 
      write('Enter first name: '),nl, 
      read(FName),nl, 
      string_chars(FName,N1),
      removeFirstLetter(N1,L1),
      charstring(L1,String1),
      atomic_list_concat( String1 , Cs1),
      
      write('Enter Middle name: '),nl, 
      read(MidName),nl,
      string_chars(MidName,N2),
      removeFirstLetter(N2,L2),
      charstring(L2,String2),
      atomic_list_concat( String2 , Cs2),
      
      write('Enter Last name: '),nl, 
      read(LName),nl,
      string_chars(LName,N3),
      removeFirstLetter(N3,L3),
      charstring(L3,String3),
      atomic_list_concat( String3 , Cs3),
      write(Cs1),write(" "),write(Cs2), write(" "),write(Cs3).
        
charstring([],[]).
    charstring([H|T],[H2|L]):-
    term_string(H,H2),
charstring(T,L).
    
removeFirstLetter([_|T],T).

Example:

?-k.

Enter first name:
peter

Enter Middle name:
jane

Enter Last name:
mary

eter ane ary
1true



回答2:


The built-in predicate read/1 reads in a prolog term, terminated by a full-stop. "peter jane mary" is not a prolog term, so you get a syntax error. In particular, prolog doesn't know what to do with the blank space between your names.

If you prefer to use comma to separate names, you can get this:

?- read(X), X = ','(A, ','(B,C)).
|: peter,mary,paul.

X =  (peter,mary,paul),
A = peter,
B = mary,
C = paul.

The comma is interpreted as a functor (in standard prolog). Have I replied your question ?



来源:https://stackoverflow.com/questions/65366635/how-to-get-the-first-letter-of-each-word-enter-by-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!