First Word
January 25, 2019
Our version uses string-index from the Standard Prelude:
(define (first-word str) (substring str 0 (string-index #\space str)))
And here’s our sample problem:
> (first-word "abcdefg hijklmnop qrs tuv wxyz") "abcdefg"
You can run the program at https://ideone.com/tfoYDE.
Extra points for implementing the Unicode default word breaking algorithm: https://unicode.org/reports/tr29/#Word_Boundaries
Two solutions in golang.
package main import ( "fmt" "regexp" ) func main() { s1 := "abcdefg hijklmnop qrs tuv wxyz" s2 := "~hola caracola de piazzolla" fmt.Println(firstWordWithLoop(s1)) fmt.Println(firstWordWithRegexp(s1)) fmt.Println() fmt.Println(firstWordWithLoop(s2)) fmt.Println(firstWordWithRegexp(s2)) } func firstWordWithLoop(str string) string { word := "" for _, char := range str { if char == ' ' { break } word += string(char) } return word } func firstWordWithRegexp(str string) string { return regexp.MustCompile(`[^ ]+`).FindString(str) }Here’s a solution in C.
#include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s STR\n", argv[0]); return EXIT_FAILURE; } char* str = argv[1]; while (1) { char c = *(str++); if (c == ' ' || c == '\0') break; printf("%c", c); } printf("\n"); return EXIT_SUCCESS; }Example Usage:
AWK version
$ echo "abcdefg hijklmnop qrs tuv wxyz" | awk ‘{ print $1 }’
abcdefg
—
Klong version
(-1)_((a?" ")@0)#a::"abcdefg hijklmnop qrs tuv wxyz"
"abcdef"
a
"abcdefg hijklmnop qrs tuv wxyz"
a?" "
[7 17 21 25]
—
MUMPS version
YDB>w $p("abcdefg hijklmnop qrs tuv wxyz"," ")
abcdefg
That Unicode algorithm looks a bit complicated, here’s a simple Unicode-friendly solution using Python str.isspace():
def firstword(s): start = -1 for i,c in enumerate(s): if start < 0: if not c.isspace(): start = i; elif c.isspace(): return s[start:i] return None if start < 0 else s[start:] assert(firstword("") is None) assert(firstword(" ") is None) assert(firstword("foo") == "foo") assert(firstword(" foo") == "foo") assert(firstword("foo ") == "foo")Python solution:
firstWord = lambda x: x.lstrip(" ").split(" ")[0]