Logo - Strings
Any sequence of alpha-numeric characters, for example – “america”, “emp1234”, etc. are examples of a string. Counting the characters is the most basic of all string processes. The answer to the question stringlength "abc12ef is given by the following procedure −
to stringlength :s make "inputstring :s make "count 0 while [not emptyp :s] [ make "count :count + 1 print first :s make "s butfirst :s ] print (sentence :inputstring "has :count "letters) end
In the above procedure –‘s’ is the variable containing the input string. Variable inputstring contains the copy of the input string. Variable count is initialized with 0. In the while loop, the condition checks whether the string has become empty or not. In each loop count, a variable is being increased by 1 to hold the length count. The statement print first :s, prints the first character only of the string stored in ‘s’.
The statement make "s butfirst :s, retrieves the sub-string excluding the first character. After exiting from the while-loop, we have printed the character count or the length of the input string. Following is the execution and output of the code.