I have got a method length(list,var) which gives me the length of a list but i want to find the length of a String, anyone with a solution ?
-
Check ["Working with strings in Prolog"](http://obvcode.blogspot.com/2008/11/working-with-strings-in-prolog.html) article – Mikita Belahlazau May 03 '12 at 12:21
-
1this article is confusing (mixes strings and atoms) and advises non standard predicates. I'd advise against reading it. I'd avise [this excellent answer](http://stackoverflow.com/questions/8264699/what-is-the-difference-between-and-in-prolog/8269897#8269897) to get what are Prolog strings instead. – m09 May 03 '12 at 13:17
5 Answers
If you would like to go ISO Prolog, then you wouldn't use
the name/2
predicate.
ISO Prolog offers you atom_codes/2
and atom_chars/2
. They offer you the
functionaliy of converting an atom back forth to either a list of codes
or a list of characters. Atoms are the strings of a Prolog system, and
characters are simply atoms of length 1. Here are some example invokations
of the two predicates:
?- atom_codes(ant, L).
L = [97,110,116]
?- atom_codes(X, [97,110,116]).
X = ant
?- atom_chars(ant, X).
X = [a,n,t]
?- atom_chars(X,[a,n,t]).
X = ant
Now you wonder how to determine the length of a string aka atom. One
way to go would be to first determine the codes or characters, and then
compute the length of this list. But ISO also offers an atom_length/2
predicate. It works as follows:
?- atom_length(ant,X).
X = 3
Many Prolog systems provide this set of built-in predicates, namely SWI Prolog, GNU Prolog, Jekejeke Prolog etc.. The available character code range and encoding depends on the Prolog system. Some allow only 8-bit codes, some 16-bit codes and some 32-bit codes.
Also the maximum length of an atom might differ, and sometimes there is an extra string type which is not tabled compared with the atom type which is often. But this extra type is usually not the same as a double quote enclosed string, which is only a shorthand for a character list.

- 137,073
- 23
- 153
- 219
Since strings are atoms, you can't manipulate them directly to get the length, extract substrings, etc. First you have to convert them to character lists with atom_chars/2
:
atom_chars(StrVar, ListVar), length(ListVar, LengthVar).

- 91,912
- 16
- 138
- 175
-
1
-
Thanks, @Mog. Updated. But as CM said, atom_length is the better option in ISO – Mark Reed May 03 '12 at 15:58
was just playing with it and i gave the predicate as
?- length("hello", R).
R = 5.
surprisingly it worked :)
-
3You can check your prolog system and query the prolog flag double_quotes. If it says codes, then double quotes are read in as character code lists. SWI-Prolog: ?- current_prolog_flag(double_quotes,X). X = codes. ?- "hello" = [104, 101, 108, 108, 111]. true. – May 03 '12 at 17:55
Maybe this:
string_length(+String, -Length)
from Prolog manual.

- 1,064
- 3
- 15
- 29
-
This is available for SWI-Prolog, but I don't know if this predicate is built-in for other Prolog systems. – Anderson Green Nov 13 '16 at 05:46
use this :
?- same_length('abhi',Z).
Z=4
if you are trying to make a .pl file for this then use this :
samelen(X ,Y) :- string_length(X,Y).
// ctrl+c & ctrl+b to save this file
//Now write this as query:
samelen(roger,Y).
Y=5

- 1
- 1