Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
10
Task/String-matching/00DESCRIPTION
Normal file
10
Task/String-matching/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{{basic data operation}}
|
||||
Given two strings, demonstrate the following 3 types of matchings:
|
||||
|
||||
# Determining if the first string starts with second string
|
||||
# Determining if the first string contains the second string at any location
|
||||
# Determining if the first string ends with the second string
|
||||
|
||||
Optional requirements:
|
||||
# Print the location of the match for part 2
|
||||
# Handle multiple occurrences of a string for part 2.
|
||||
2
Task/String-matching/00META.yaml
Normal file
2
Task/String-matching/00META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: String manipulation
|
||||
18
Task/String-matching/ALGOL-68/string-matching.alg
Normal file
18
Task/String-matching/ALGOL-68/string-matching.alg
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# define some appropriate OPerators #
|
||||
PRIO STARTSWITH = 5, ENDSWITH = 5;
|
||||
OP STARTSWITH = (STRING str, prefix)BOOL: # assuming LWB = 1 #
|
||||
IF UPB str < UPB prefix THEN FALSE ELSE str[:UPB prefix]=prefix FI;
|
||||
OP ENDSWITH = (STRING str, suffix)BOOL: # assuming LWB = 1 #
|
||||
IF UPB str < UPB suffix THEN FALSE ELSE str[UPB str-UPB suffix+1:]=suffix FI;
|
||||
|
||||
INT loc, loc2;
|
||||
|
||||
print((
|
||||
"abcd" STARTSWITH "ab", # returns TRUE #
|
||||
"abcd" ENDSWITH "zn", # returns FALSE #
|
||||
string in string("bb",loc,"abab"), # returns FALSE #
|
||||
string in string("ab",loc,"abab"), # returns TRUE #
|
||||
(string in string("bb",loc,"abab")|loc|-1), # returns -1 #
|
||||
(string in string("ab",loc,"abab")|loc|-1), # returns +1 #
|
||||
(string in string("ab",loc2,"abab"[loc+1:])|loc+loc2|-1) # returns +3 #
|
||||
))
|
||||
20
Task/String-matching/AWK/string-matching.awk
Normal file
20
Task/String-matching/AWK/string-matching.awk
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/awk -f
|
||||
{
|
||||
if ($1 ~ "^"$2) {
|
||||
print $1" begins with "$2;
|
||||
} else {
|
||||
print $1" does not begin with "$2;
|
||||
}
|
||||
|
||||
if ($1 ~ $2) {
|
||||
print $1" contains "$2;
|
||||
} else {
|
||||
print $1" does not contain "$2;
|
||||
}
|
||||
|
||||
if ($1 ~ $2"$") {
|
||||
print $1" ends with "$2;
|
||||
} else {
|
||||
print $1" does not end with "$2;
|
||||
}
|
||||
}
|
||||
20
Task/String-matching/Ada/string-matching.ada
Normal file
20
Task/String-matching/Ada/string-matching.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Match_Strings is
|
||||
S1 : constant String := "abcd";
|
||||
S2 : constant String := "abab";
|
||||
S3 : constant String := "ab";
|
||||
begin
|
||||
if S1'Length >= S3'Length and then S1 (S1'First..S1'First + S3'Length - 1) = S3 then
|
||||
Put_Line (''' & S1 & "' starts with '" & S3 & ''');
|
||||
end if;
|
||||
if S2'Length >= S3'Length and then S2 (S2'Last - S3'Length + 1..S2'Last) = S3 then
|
||||
Put_Line (''' & S2 & "' ends with '" & S3 & ''');
|
||||
end if;
|
||||
Put_Line (''' & S3 & "' first appears in '" & S1 & "' at" & Integer'Image (Index (S1, S3)));
|
||||
Put_Line
|
||||
( ''' & S3 & "' appears in '" & S2 & ''' &
|
||||
Integer'Image (Ada.Strings.Fixed.Count (S2, S3)) & " times"
|
||||
);
|
||||
end Match_Strings;
|
||||
23
Task/String-matching/Aime/string-matching.aime
Normal file
23
Task/String-matching/Aime/string-matching.aime
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
text s, t;
|
||||
data b;
|
||||
|
||||
s = "Bangkok";
|
||||
t = "Bang";
|
||||
|
||||
b_cast(b, s);
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b_seek(b, t) == 0,
|
||||
b_seek(b, t) != -1,
|
||||
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));
|
||||
|
||||
t = "ok";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b_seek(b, t) == 0,
|
||||
b_seek(b, t) != -1,
|
||||
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));
|
||||
|
||||
t = "Summer";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b_seek(b, t) == 0,
|
||||
b_seek(b, t) != -1,
|
||||
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));
|
||||
14
Task/String-matching/AutoHotkey/string-matching.ahk
Normal file
14
Task/String-matching/AutoHotkey/string-matching.ahk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
String1 = abcd
|
||||
String2 = abab
|
||||
|
||||
If (SubStr(String1,1,StrLen(String2)) = String2)
|
||||
MsgBox, "%String1%" starts with "%String2%".
|
||||
IfInString, String1, %String2%
|
||||
{
|
||||
Position := InStr(String1,String2)
|
||||
StringReplace, String1, String1, %String2%, %String2%, UseErrorLevel
|
||||
MsgBox, "%String1%" contains "%String2%" at position %Position%`, and appears %ErrorLevel% times.
|
||||
}
|
||||
StringRight, TempVar, String1, StrLen(String2)
|
||||
If TempVar = %String2%
|
||||
MsgBox, "%String1%" ends with "%String2%".
|
||||
29
Task/String-matching/AutoIt/string-matching.autoit
Normal file
29
Task/String-matching/AutoIt/string-matching.autoit
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
$string1 = "arduinoardblobard"
|
||||
$string2 = "ard"
|
||||
|
||||
; == Determining if the first string starts with second string
|
||||
If StringLeft($string1, StringLen($string2)) = $string2 Then
|
||||
ConsoleWrite("1st string starts with 2nd string." & @CRLF)
|
||||
Else
|
||||
ConsoleWrite("1st string does'nt starts with 2nd string." & @CRLF)
|
||||
EndIf
|
||||
|
||||
; == Determining if the first string contains the second string at any location
|
||||
; == Print the location of the match for part 2
|
||||
; == Handle multiple occurrences of a string for part 2
|
||||
$start = 1
|
||||
$count = 0
|
||||
$pos = StringInStr($string1, $string2)
|
||||
While $pos
|
||||
$count += 1
|
||||
ConsoleWrite("1st string contains 2nd string at position: " & $pos & @CRLF)
|
||||
$pos = StringInStr($string1, $string2, 0, 1, $start + $pos + StringLen($string2))
|
||||
WEnd
|
||||
If $count = 0 Then ConsoleWrite("1st string does'nt contain 2nd string." & @CRLF)
|
||||
|
||||
; == Determining if the first string ends with the second string
|
||||
If StringRight($string1, StringLen($string2)) = $string2 Then
|
||||
ConsoleWrite("1st string ends with 2nd string." & @CRLF)
|
||||
Else
|
||||
ConsoleWrite("1st string does'nt ends with 2nd string." & @CRLF)
|
||||
EndIf
|
||||
27
Task/String-matching/BASIC/string-matching-1.basic
Normal file
27
Task/String-matching/BASIC/string-matching-1.basic
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
first$ = "qwertyuiop"
|
||||
|
||||
'Determining if the first string starts with second string
|
||||
second$ = "qwerty"
|
||||
IF LEFT$(first$, LEN(second$)) = second$ THEN
|
||||
PRINT "'"; first$; "' starts with '"; second$; "'"
|
||||
ELSE
|
||||
PRINT "'"; first$; "' does not start with '"; second$; "'"
|
||||
END IF
|
||||
|
||||
'Determining if the first string contains the second string at any location
|
||||
'Print the location of the match for part 2
|
||||
second$ = "wert"
|
||||
x = INSTR(first$, second$)
|
||||
IF x THEN
|
||||
PRINT "'"; first$; "' contains '"; second$; "' at position "; x
|
||||
ELSE
|
||||
PRINT "'"; first$; "' does not contain '"; second$; "'"
|
||||
END IF
|
||||
|
||||
' Determining if the first string ends with the second string
|
||||
second$ = "random garbage"
|
||||
IF RIGHT$(first$, LEN(second$)) = second$ THEN
|
||||
PRINT "'"; first$; "' ends with '"; second$; "'"
|
||||
ELSE
|
||||
PRINT "'"; first$; "' does not end with '"; second$; "'"
|
||||
END IF
|
||||
18
Task/String-matching/BASIC/string-matching-2.basic
Normal file
18
Task/String-matching/BASIC/string-matching-2.basic
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
10 A$ = "THIS, THAT, AND THE OTHER THING"
|
||||
20 S$ = "TH"
|
||||
30 DEF FN S(P) = MID$(A$, P, LEN(S$)) = S$
|
||||
40 PRINT A$ : PRINT
|
||||
|
||||
110 S$(1) = "STARTS"
|
||||
120 S$(0) = "DOES NOT START"
|
||||
130 PRINT S$(FN S(1))" WITH "S$ : PRINT
|
||||
|
||||
210 R$ = "" : FOR I = 1 TO LEN(A$) - LEN(S$) : IF FN S(I) THEN R$ = R$ + STR$(I) + " "
|
||||
220 NEXT I
|
||||
230 IF LEN(R$) = 0 THEN PRINT "DOES NOT CONTAIN "S$
|
||||
240 IF LEN(R$) THEN PRINT "CONTAINS "S$" LOCATED AT POSITION "R$
|
||||
250 PRINT
|
||||
|
||||
310 E$(1) = "ENDS"
|
||||
320 E$(0) = "DOES NOT END"
|
||||
330 PRINT E$(FN S(LEN(A$) - LEN(S$) + 1))" WITH "S$
|
||||
32
Task/String-matching/BBC-BASIC/string-matching.bbc
Normal file
32
Task/String-matching/BBC-BASIC/string-matching.bbc
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
first$ = "The fox jumps over the dog"
|
||||
|
||||
FOR test% = 1 TO 3
|
||||
READ second$
|
||||
starts% = FN_first_starts_with_second(first$, second$)
|
||||
IF starts% PRINT """" first$ """ starts with """ second$ """"
|
||||
ends% = FN_first_ends_with_second(first$, second$)
|
||||
IF ends% PRINT """" first$ """ ends with """ second$ """"
|
||||
where% = FN_first_contains_second_where(first$, second$)
|
||||
IF where% PRINT """" first$ """ contains """ second$ """ at position " ; where%
|
||||
howmany% = FN_first_contains_second_howmany(first$, second$)
|
||||
IF howmany% PRINT """" first$ """ contains """ second$ """ " ; howmany% " time(s)"
|
||||
NEXT
|
||||
DATA "The", "he", "dog"
|
||||
END
|
||||
|
||||
DEF FN_first_starts_with_second(A$, B$)
|
||||
= B$ = LEFT$(A$, LEN(B$))
|
||||
|
||||
DEF FN_first_ends_with_second(A$, B$)
|
||||
= B$ = RIGHT$(A$, LEN(B$))
|
||||
|
||||
DEF FN_first_contains_second_where(A$, B$)
|
||||
= INSTR(A$, B$)
|
||||
|
||||
DEF FN_first_contains_second_howmany(A$, B$)
|
||||
LOCAL I%, N% : I% = 0
|
||||
REPEAT
|
||||
I% = INSTR(A$, B$, I%+1)
|
||||
IF I% THEN N% += 1
|
||||
UNTIL I% = 0
|
||||
= N%
|
||||
9
Task/String-matching/Bracmat/string-matching.bracmat
Normal file
9
Task/String-matching/Bracmat/string-matching.bracmat
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
( (sentence="I want a number such that that number will be even.")
|
||||
& out$(@(!sentence:I ?) & "sentence starts with 'I'" | "sentence does not start with 'I'")
|
||||
& out$(@(!sentence:? such ?) & "sentence contains 'such'" | "sentence does not contain 'such'")
|
||||
& out$(@(!sentence:? "even.") & "sentence ends with 'even.'" | "sentence does not end with 'even.'")
|
||||
& 0:?N
|
||||
& ( @(!sentence:? be (? & !N+1:?N & ~))
|
||||
| out$str$("sentence contains " !N " occurrences of 'be'")
|
||||
)
|
||||
)
|
||||
14
Task/String-matching/C++/string-matching.cpp
Normal file
14
Task/String-matching/C++/string-matching.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
string s1="abcd";
|
||||
string s2="abab";
|
||||
string s3="ab";
|
||||
//Beginning
|
||||
s1.compare(0,s3.size(),s3)==0;
|
||||
//End
|
||||
s1.compare(s1.size()-s3.size(),s3.size(),s3)==0;
|
||||
//Anywhere
|
||||
s1.find(s2)//returns string::npos
|
||||
int loc=s2.find(s3)//returns 0
|
||||
loc=s2.find(s3,loc+1)//returns 2
|
||||
32
Task/String-matching/C/string-matching-1.c
Normal file
32
Task/String-matching/C/string-matching-1.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int startsWith(const char* container, const char* target)
|
||||
{
|
||||
size_t clen = strlen(container), tlen = strlen(target);
|
||||
if (clen < tlen)
|
||||
return 0;
|
||||
return strncmp(container, target, tlen) == 0;
|
||||
}
|
||||
|
||||
int endsWith(const char* container, const char* target)
|
||||
{
|
||||
size_t clen = strlen(container), tlen = strlen(target);
|
||||
if (clen < tlen)
|
||||
return 0;
|
||||
return strncmp(container + clen - tlen, target, tlen) == 0;
|
||||
}
|
||||
|
||||
int doesContain(const char* container, const char* target)
|
||||
{
|
||||
return strstr(container, target) != 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Starts with Test ( Hello,Hell ) : %d\n", startsWith("Hello","Hell"));
|
||||
printf("Ends with Test ( Code,ode ) : %d\n", endsWith("Code","ode"));
|
||||
printf("Contains Test ( Google,msn ) : %d\n", doesContain("Google","msn"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
44
Task/String-matching/C/string-matching-2.c
Normal file
44
Task/String-matching/C/string-matching-2.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* returns 0 if no match, 1 if matched, -1 if matched and at end */
|
||||
int s_cmp(const char *a, const char *b)
|
||||
{
|
||||
char c1 = 0, c2 = 0;
|
||||
while (c1 == c2) {
|
||||
c1 = *(a++);
|
||||
if ('\0' == (c2 = *(b++)))
|
||||
return c1 == '\0' ? -1 : 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* returns times matched */
|
||||
int s_match(const char *a, const char *b)
|
||||
{
|
||||
int i = 0, count = 0;
|
||||
printf("matching `%s' with `%s':\n", a, b);
|
||||
|
||||
while (a[i] != '\0') {
|
||||
switch (s_cmp(a + i, b)) {
|
||||
case -1:
|
||||
printf("matched: pos %d (at end)\n\n", i);
|
||||
return ++count;
|
||||
case 1:
|
||||
printf("matched: pos %d\n", i);
|
||||
++count;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
printf("end match\n\n");
|
||||
return count;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
s_match("A Short String", "ort S");
|
||||
s_match("aBaBaBaBa", "aBa");
|
||||
s_match("something random", "Rand");
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
Task/String-matching/Clojure/string-matching.clj
Normal file
10
Task/String-matching/Clojure/string-matching.clj
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(def evals '((. "abcd" startsWith "ab")
|
||||
(. "abcd" endsWith "zn")
|
||||
(. "abab" contains "bb")
|
||||
(. "abab" contains "ab")
|
||||
(. "abab" indexOf "bb")
|
||||
(let [loc (. "abab" indexOf "ab")]
|
||||
(. "abab" indexOf "ab" (dec loc)))))
|
||||
|
||||
user> (for [i evals] [i (eval i)])
|
||||
([(. "abcd" startsWith "ab") true] [(. "abcd" endsWith "zn") false] [(. "abab" contains "bb") false] [(. "abab" contains "ab") true] [(. "abab" indexOf "bb") -1] [(let [loc (. "abab" indexOf "ab")] (. "abab" indexOf "ab" (dec loc))) 0])
|
||||
20
Task/String-matching/CoffeeScript/string-matching.coffee
Normal file
20
Task/String-matching/CoffeeScript/string-matching.coffee
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
matchAt = (s, frag, i) ->
|
||||
s[i...i+frag.length] == frag
|
||||
|
||||
startsWith = (s, frag) ->
|
||||
matchAt s, frag, 0
|
||||
|
||||
endsWith = (s, frag) ->
|
||||
matchAt s, frag, s.length - frag.length
|
||||
|
||||
matchLocations = (s, frag) ->
|
||||
(i for i in [0..s.length - frag.length] when matchAt s, frag, i)
|
||||
|
||||
console.log startsWith "tacoloco", "taco" # true
|
||||
console.log startsWith "taco", "tacoloco" # false
|
||||
console.log startsWith "tacoloco", "talk" # false
|
||||
console.log endsWith "tacoloco", "loco" # true
|
||||
console.log endsWith "loco", "tacoloco" # false
|
||||
console.log endsWith "tacoloco", "yoco" # false
|
||||
console.log matchLocations "bababab", "bab" # [0,2,4]
|
||||
console.log matchLocations "xxx", "x" # [0,1,2]
|
||||
28
Task/String-matching/Common-Lisp/string-matching.lisp
Normal file
28
Task/String-matching/Common-Lisp/string-matching.lisp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(defun starts-with-p (str1 str2)
|
||||
"Determine whether `str1` starts with `str2`"
|
||||
(let ((p (search str2 str1)))
|
||||
(and p (= 0 p))))
|
||||
|
||||
(print (starts-with-p "foobar" "foo")) ; T
|
||||
(print (starts-with-p "foobar" "bar")) ; NIL
|
||||
|
||||
(defun ends-with-p (str1 str2)
|
||||
"Determine whether `str1` ends with `str2`"
|
||||
(let ((p (mismatch str2 str1 :from-end T)))
|
||||
(or (not p) (= 0 p))))
|
||||
|
||||
(print (ends-with-p "foobar" "foo")) ; NIL
|
||||
(print (ends-with-p "foobar" "bar")) ; T
|
||||
|
||||
(defun containsp (str1 str2)
|
||||
"Determine whether `str1` contains `str2`.
|
||||
Instead of just returning T, return a list of starting locations
|
||||
for every occurence of `str2` in `str1`"
|
||||
(unless (string-equal str2 "")
|
||||
(loop for p = (search str2 str1) then (search str2 str1 :start2 (1+ p))
|
||||
while p
|
||||
collect p)))
|
||||
|
||||
(print (containsp "foobar" "oba")) ; (2)
|
||||
(print (containsp "ababaBa" "ba")) ; (1 3)
|
||||
(print (containsp "foobar" "x")) ; NIL
|
||||
113
Task/String-matching/Component-Pascal/string-matching.component
Normal file
113
Task/String-matching/Component-Pascal/string-matching.component
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
MODULE StringMatch;
|
||||
IMPORT StdLog,Strings;
|
||||
CONST
|
||||
strSize = 1024;
|
||||
patSize = 256;
|
||||
|
||||
TYPE
|
||||
Matcher* = POINTER TO LIMITED RECORD
|
||||
str: ARRAY strSize OF CHAR;
|
||||
pat: ARRAY patSize OF CHAR;
|
||||
pos: INTEGER
|
||||
END;
|
||||
|
||||
PROCEDURE NewMatcher*(IN str: ARRAY OF CHAR): Matcher;
|
||||
VAR
|
||||
m: Matcher;
|
||||
BEGIN
|
||||
NEW(m);m.str := str$;m.pos:= 0;
|
||||
RETURN m
|
||||
END NewMatcher;
|
||||
|
||||
PROCEDURE (m: Matcher) Match*(IN pat: ARRAY OF CHAR): INTEGER,NEW;
|
||||
VAR
|
||||
pos: INTEGER;
|
||||
BEGIN
|
||||
m.pat := pat$;
|
||||
pos := m.pos;
|
||||
Strings.Find(m.str,m.pat,pos,m.pos);
|
||||
RETURN m.pos
|
||||
END Match;
|
||||
|
||||
PROCEDURE (m: Matcher) Next*(): INTEGER, NEW;
|
||||
VAR
|
||||
pos: INTEGER;
|
||||
BEGIN
|
||||
pos := m.pos + LEN(m.pat$);
|
||||
Strings.Find(m.str,m.pat,pos,m.pos);
|
||||
RETURN m.pos;
|
||||
END Next;
|
||||
|
||||
(* Some Helper functions based on Strings module *)
|
||||
PROCEDURE StartsWith(IN str: ARRAY OF CHAR;IN pat: ARRAY OF CHAR): BOOLEAN;
|
||||
VAR
|
||||
pos: INTEGER;
|
||||
BEGIN
|
||||
Strings.Find(str,pat,0,pos);
|
||||
RETURN pos = 0
|
||||
END StartsWith;
|
||||
|
||||
PROCEDURE Contains(IN str: ARRAY OF CHAR;IN pat: ARRAY OF CHAR; OUT pos: INTEGER): BOOLEAN;
|
||||
BEGIN
|
||||
Strings.Find(str,pat,0,pos);
|
||||
RETURN pos >= 0
|
||||
END Contains;
|
||||
|
||||
PROCEDURE EndsWith(IN str: ARRAY OF CHAR;IN pat: ARRAY OF CHAR): BOOLEAN;
|
||||
VAR
|
||||
pos: INTEGER;
|
||||
BEGIN
|
||||
Strings.Find(str,pat,0,pos);
|
||||
RETURN pos + LEN(pat$) = LEN(str$)
|
||||
END EndsWith;
|
||||
|
||||
PROCEDURE Do*;
|
||||
CONST
|
||||
aStr = "abcdefghijklmnopqrstuvwxyz";
|
||||
VAR
|
||||
pat: ARRAY 128 OF CHAR;
|
||||
res: BOOLEAN;
|
||||
at: INTEGER;
|
||||
m: Matcher;
|
||||
BEGIN
|
||||
(* StartsWith *)
|
||||
pat := "abc";
|
||||
StdLog.String(aStr + " startsWith " + pat + " :>");StdLog.Bool(StartsWith(aStr,pat));StdLog.Ln;
|
||||
pat := "cba";
|
||||
StdLog.String(aStr + " startsWith " + pat + " :>");StdLog.Bool(StartsWith(aStr,pat));StdLog.Ln;
|
||||
pat := "def";
|
||||
StdLog.String(aStr + " startsWith " + pat + " :>");StdLog.Bool(StartsWith(aStr,pat));StdLog.Ln;
|
||||
StdLog.Ln;
|
||||
(* Contains *)
|
||||
pat := 'def';
|
||||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at));
|
||||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln;
|
||||
pat := 'efd';
|
||||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at));
|
||||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln;
|
||||
pat := 'abc';
|
||||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at));
|
||||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln;
|
||||
pat := 'xyz';
|
||||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at));
|
||||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln;
|
||||
StdLog.Ln;
|
||||
(* EndsWith *)
|
||||
pat := 'xyz';
|
||||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln;
|
||||
pat := 'zyx';
|
||||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln;
|
||||
pat := 'abc';
|
||||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln;
|
||||
pat:= 'def';
|
||||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln;
|
||||
StdLog.Ln;
|
||||
|
||||
m := NewMatcher("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
|
||||
StdLog.String("Matching 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' against 'abc':> ");
|
||||
StdLog.Ln;
|
||||
StdLog.String("Match at: ");StdLog.Int(m.Match("abc"));StdLog.Ln;
|
||||
StdLog.String("Match at: ");StdLog.Int(m.Next());StdLog.Ln;
|
||||
StdLog.String("Match at: ");StdLog.Int(m.Next());StdLog.Ln
|
||||
END Do;
|
||||
END StringMatch.
|
||||
16
Task/String-matching/D/string-matching.d
Normal file
16
Task/String-matching/D/string-matching.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
void main() {
|
||||
import std.stdio;
|
||||
import std.algorithm: startsWith, endsWith, find, countUntil;
|
||||
|
||||
"abcd".startsWith("ab").writeln; // true
|
||||
"abcd".endsWith("zn").writeln; // false
|
||||
"abab".find("bb").writeln; // empty array (no match)
|
||||
"abcd".find("bc").writeln; // "bcd" (substring start
|
||||
// at match)
|
||||
"abab".countUntil("bb").writeln; // -1 (no match)
|
||||
"abab".countUntil("ba").writeln; // 1 (index of 1st match)
|
||||
|
||||
// std.algorithm.startsWith also works on arrays and ranges:
|
||||
[1, 2, 3].countUntil(3).writeln; // 2
|
||||
[1, 2, 3].countUntil([2, 3]).writeln; // 1
|
||||
}
|
||||
13
Task/String-matching/Delphi/string-matching.delphi
Normal file
13
Task/String-matching/Delphi/string-matching.delphi
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
program CharacterMatching;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses StrUtils;
|
||||
|
||||
begin
|
||||
WriteLn(AnsiStartsText('ab', 'abcd')); // True
|
||||
WriteLn(AnsiEndsText('zn', 'abcd')); // False
|
||||
WriteLn(AnsiContainsText('abcd', 'bb')); // False
|
||||
Writeln(AnsiContainsText('abcd', 'ab')); // True
|
||||
WriteLn(Pos('ab', 'abcd')); // 1
|
||||
end.
|
||||
11
Task/String-matching/E/string-matching.e
Normal file
11
Task/String-matching/E/string-matching.e
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def f(string1, string2) {
|
||||
println(string1.startsWith(string2))
|
||||
|
||||
var index := 0
|
||||
while ((index := string1.startOf(string2, index)) != -1) {
|
||||
println(`at $index`)
|
||||
index += 1
|
||||
}
|
||||
|
||||
println(string1.endsWith(string2))
|
||||
}
|
||||
22
Task/String-matching/Elena/string-matching.elena
Normal file
22
Task/String-matching/Elena/string-matching.elena
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#define system'routines.
|
||||
#define extensions.
|
||||
|
||||
// --- Program ---
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
literalControl starting:"hello" &with:"hel" ?
|
||||
[
|
||||
consoleEx writeLine:"hello starts with hel".
|
||||
].
|
||||
|
||||
literalControl ending:"hello" &with:"llo" ?
|
||||
[
|
||||
consoleEx writeLine:"hello ends with llo".
|
||||
].
|
||||
|
||||
literalControl contain:"el" &in:"hello" ?
|
||||
[
|
||||
consoleEx writeLine:"hello contains el".
|
||||
].
|
||||
].
|
||||
18
Task/String-matching/Elixir/string-matching.elixir
Normal file
18
Task/String-matching/Elixir/string-matching.elixir
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
s1 = "abcd"
|
||||
s2 = "adab"
|
||||
s3 = "ab"
|
||||
|
||||
String.starts_with?(s1, s3)
|
||||
# => true
|
||||
String.starts_with?(s2, s3)
|
||||
# => false
|
||||
|
||||
String.ends_with?(s1, s3)
|
||||
# => false
|
||||
String.ends_with?(s2, s3)
|
||||
# => true
|
||||
|
||||
String.contains?(s1, s3)
|
||||
# => true
|
||||
String.contains?(s2, s3)
|
||||
# => true
|
||||
28
Task/String-matching/Erlang/string-matching.erl
Normal file
28
Task/String-matching/Erlang/string-matching.erl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
-module(character_matching).
|
||||
-export([starts_with/2,ends_with/2,contains/2]).
|
||||
|
||||
%% Both starts_with and ends_with are mappings to 'lists:prefix/2' and
|
||||
%% 'lists:suffix/2', respectively.
|
||||
|
||||
starts_with(S1,S2) ->
|
||||
lists:prefix(S2,S1).
|
||||
|
||||
ends_with(S1,S2) ->
|
||||
lists:suffix(S2,S1).
|
||||
|
||||
contains(S1,S2) ->
|
||||
contains(S1,S2,1,[]).
|
||||
|
||||
%% While S1 is at least as long as S2 we check if S2 is its prefix,
|
||||
%% storing the result if it is. When S1 is shorter than S2, we return
|
||||
%% the result. NB: this will work on any arbitrary list in erlang
|
||||
%% since it makes no distinction between strings and lists.
|
||||
contains([_|T]=S1,S2,N,Acc) when length(S2) =< length(S1) ->
|
||||
case starts_with(S1,S2) of
|
||||
true ->
|
||||
contains(T,S2,N+1,[N|Acc]);
|
||||
false ->
|
||||
contains(T,S2,N+1,Acc)
|
||||
end;
|
||||
contains(_S1,_S2,_N,Acc) ->
|
||||
Acc.
|
||||
30
Task/String-matching/Euphoria/string-matching.euphoria
Normal file
30
Task/String-matching/Euphoria/string-matching.euphoria
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
sequence first, second
|
||||
integer x
|
||||
|
||||
first = "qwertyuiop"
|
||||
|
||||
-- Determining if the first string starts with second string
|
||||
second = "qwerty"
|
||||
if match(second, first) = 1 then
|
||||
printf(1, "'%s' starts with '%s'\n", {first, second})
|
||||
else
|
||||
printf(1, "'%s' does not start with '%s'\n", {first, second})
|
||||
end if
|
||||
|
||||
-- Determining if the first string contains the second string at any location
|
||||
-- Print the location of the match for part 2
|
||||
second = "wert"
|
||||
x = match(second, first)
|
||||
if x then
|
||||
printf(1, "'%s' contains '%s' at position %d\n", {first, second, x})
|
||||
else
|
||||
printf(1, "'%s' does not contain '%s'\n", {first, second})
|
||||
end if
|
||||
|
||||
-- Determining if the first string ends with the second string
|
||||
second = "uio"
|
||||
if length(second)<=length(first) and match_from(second, first, length(first)-length(second)+1) then
|
||||
printf(1, "'%s' ends with '%s'\n", {first, second})
|
||||
else
|
||||
printf(1, "'%s' does not end with '%s'\n", {first, second})
|
||||
end if
|
||||
15
Task/String-matching/FBSL/string-matching.fbsl
Normal file
15
Task/String-matching/FBSL/string-matching.fbsl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#APPTYPE CONSOLE
|
||||
|
||||
DIM s = "roko, mat jane do"
|
||||
|
||||
IF LEFT(s, 4) = "roko" THEN PRINT STRENC(s), " starts with ", STRENC("roko")
|
||||
IF INSTR(s, "mat") THEN PRINT STRENC(s), " contains ", STRENC("mat"), " at ", INSTR
|
||||
IF RIGHT(s, 2) = "do" THEN PRINT STRENC(s), " ends with ", STRENC("do")
|
||||
PRINT STRENC(s), " contains ", STRENC("o"), " at the following locations:", InstrEx(s, "o")
|
||||
|
||||
PAUSE
|
||||
|
||||
SUB InstrEx(mane, match)
|
||||
INSTR = 0
|
||||
WHILE INSTR(mane, match, INSTR + 1): PRINT " ", INSTR;: WEND
|
||||
END SUB
|
||||
22
Task/String-matching/Fantom/string-matching.fantom
Normal file
22
Task/String-matching/Fantom/string-matching.fantom
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
string := "Fantom Language"
|
||||
echo ("String is: " + string)
|
||||
echo ("does string start with 'Fantom'? " + string.startsWith("Fantom"))
|
||||
echo ("does string start with 'Language'? " + string.startsWith("Language"))
|
||||
echo ("does string contain 'age'? " + string.contains("age"))
|
||||
echo ("does string contain 'page'? " + string.contains("page"))
|
||||
echo ("does string end with 'Fantom'? " + string.endsWith("Fantom"))
|
||||
echo ("does string end with 'Language'? " + string.endsWith("Language"))
|
||||
|
||||
echo ("Location of 'age' is: " + string.index("age"))
|
||||
posn := string.index ("an")
|
||||
echo ("First location of 'an' is: " + posn)
|
||||
posn = string.index ("an", posn+1)
|
||||
echo ("Second location of 'an' is: " + posn)
|
||||
posn = string.index ("an", posn+1)
|
||||
if (posn == null) echo ("No third location of 'an'")
|
||||
}
|
||||
}
|
||||
5
Task/String-matching/Forth/string-matching.fth
Normal file
5
Task/String-matching/Forth/string-matching.fth
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
: starts-with ( a l a2 l2 -- ? )
|
||||
tuck 2>r min 2r> compare 0= ;
|
||||
: ends-with ( a l a2 l2 -- ? )
|
||||
tuck 2>r negate over + 0 max /string 2r> compare 0= ;
|
||||
\ use SEARCH ( a l a2 l2 -- a3 l3 ? ) for contains
|
||||
35
Task/String-matching/GML/string-matching.gml
Normal file
35
Task/String-matching/GML/string-matching.gml
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#define charMatch
|
||||
{
|
||||
first = "qwertyuiop";
|
||||
|
||||
// Determining if the first string starts with second string
|
||||
second = "qwerty";
|
||||
if (string_pos(second, first) > 0) {
|
||||
show_message("'" + first + "' starts with '" + second + "'");
|
||||
} else {
|
||||
show_message("'" + first + "' does not start with '" + second + "'");
|
||||
}
|
||||
|
||||
second = "wert"
|
||||
// Determining if the first string contains the second string at any location
|
||||
// Print the location of the match for part 2
|
||||
if (string_pos(second, first) > 0) {
|
||||
show_message("'" + first + "' contains '" + second + "' at position " + string(x));
|
||||
} else {
|
||||
show_message("'" + first + "' does not contain '" + second + "'");
|
||||
}
|
||||
// Handle multiple occurrences of a string for part 2.
|
||||
x = string_count(second, first);
|
||||
show_message("'" + first + "' contains " + string(x) + " instances of '" + second + "'");
|
||||
|
||||
// Determining if the first string ends with the second string
|
||||
second = "random garbage"
|
||||
temp = string_copy(first,
|
||||
(string_length(first) - string_length(second)) + 1,
|
||||
string_length(second));
|
||||
if (temp == second) {
|
||||
show_message("'" + first + "' ends with '" + second + "'");
|
||||
} else {
|
||||
show_message("'" + first + "' does not end with '" + second + "'");
|
||||
}
|
||||
}
|
||||
30
Task/String-matching/Go/string-matching.go
Normal file
30
Task/String-matching/Go/string-matching.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func match(first, second string) {
|
||||
fmt.Printf("1. %s starts with %s: %t\n",
|
||||
first, second, strings.HasPrefix(first, second))
|
||||
i := strings.Index(first, second)
|
||||
fmt.Printf("2. %s contains %s: %t,\n", first, second, i >= 0)
|
||||
if i >= 0 {
|
||||
fmt.Printf("2.1. at location %d,\n", i)
|
||||
for start := i+1;; {
|
||||
if i = strings.Index(first[start:], second); i < 0 {
|
||||
break
|
||||
}
|
||||
fmt.Printf("2.2. at location %d,\n", start+i)
|
||||
start += i+1
|
||||
}
|
||||
fmt.Println("2.2. and that's all")
|
||||
}
|
||||
fmt.Printf("3. %s ends with %s: %t\n",
|
||||
first, second, strings.HasSuffix(first, second))
|
||||
}
|
||||
|
||||
func main() {
|
||||
match("abracadabra", "abr")
|
||||
}
|
||||
23
Task/String-matching/Groovy/string-matching.groovy
Normal file
23
Task/String-matching/Groovy/string-matching.groovy
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
assert "abcd".startsWith("ab")
|
||||
assert ! "abcd".startsWith("zn")
|
||||
assert "abcd".endsWith("cd")
|
||||
assert ! "abcd".endsWith("zn")
|
||||
assert "abab".contains("ba")
|
||||
assert ! "abab".contains("bb")
|
||||
|
||||
|
||||
assert "abab".indexOf("bb") == -1 // not found flag
|
||||
assert "abab".indexOf("ab") == 0
|
||||
|
||||
def indicesOf = { string, substring ->
|
||||
if (!string) { return [] }
|
||||
def indices = [-1]
|
||||
while (true) {
|
||||
indices << string.indexOf(substring, indices.last()+1)
|
||||
if (indices.last() == -1) break
|
||||
}
|
||||
indices[1..<(indices.size()-1)]
|
||||
}
|
||||
assert indicesOf("abab", "ab") == [0, 2]
|
||||
assert indicesOf("abab", "ba") == [1]
|
||||
assert indicesOf("abab", "xy") == []
|
||||
12
Task/String-matching/Haskell/string-matching-1.hs
Normal file
12
Task/String-matching/Haskell/string-matching-1.hs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
> import Data.List
|
||||
> "abc" `isPrefixOf` "abcdefg"
|
||||
True
|
||||
> "efg" `isSuffixOf` "abcdefg"
|
||||
True
|
||||
> "bcd" `isInfixOf` "abcdefg"
|
||||
True
|
||||
> "abc" `isInfixOf` "abcdefg" -- Prefixes and suffixes are also infixes
|
||||
True
|
||||
> let infixes a b = findIndices (isPrefixOf a) $ tails b
|
||||
> infixes "ab" "abcdefabqqab"
|
||||
[0,6,10]
|
||||
10
Task/String-matching/Haskell/string-matching-2.hs
Normal file
10
Task/String-matching/Haskell/string-matching-2.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
procedure main()
|
||||
|
||||
write("Matching s2 :=",image(s2 := "ab")," within s1:= ",image(s1 := "abcdabab"))
|
||||
|
||||
write("Test #1 beginning ",if match(s2,s1) then "matches " else "failed")
|
||||
writes("Test #2 all matches at positions [")
|
||||
every writes(" ",find(s2,s1)|"]\n")
|
||||
write("Test #3 ending ", if s1[0-:*s2] == s2 then "matches" else "fails")
|
||||
|
||||
end
|
||||
3
Task/String-matching/J/string-matching-1.j
Normal file
3
Task/String-matching/J/string-matching-1.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
startswith=: ] -: ({.~ #)
|
||||
contains=: +./@:E.~
|
||||
endswith=: ] -: ({.~ -@#)
|
||||
18
Task/String-matching/J/string-matching-2.j
Normal file
18
Task/String-matching/J/string-matching-2.j
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
'abcd' startswith 'ab'
|
||||
1
|
||||
'abcd' startswith 'cd'
|
||||
0
|
||||
'abcd' endswith 'ab'
|
||||
0
|
||||
'abcd' endswith 'cd'
|
||||
1
|
||||
'abcd' contains 'bb'
|
||||
0
|
||||
'abcd' contains 'ab'
|
||||
1
|
||||
'abcd' contains 'bc'
|
||||
1
|
||||
'abab' contains 'ab'
|
||||
1
|
||||
'abab' I.@E.~ 'ab' NB. find starting indicies
|
||||
0 2
|
||||
6
Task/String-matching/J/string-matching-3.j
Normal file
6
Task/String-matching/J/string-matching-3.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
0 1 2 3 startswith 0 1 NB. integer
|
||||
1
|
||||
4.2 5.1 1.3 9 3 contains 1.3 4.2 NB. floating point
|
||||
0
|
||||
4.2 5.1 1.3 4.2 9 3 contains 1.3 4.2
|
||||
1
|
||||
7
Task/String-matching/Java/string-matching.java
Normal file
7
Task/String-matching/Java/string-matching.java
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
"abcd".startsWith("ab") //returns true
|
||||
"abcd".endsWith("zn") //returns false
|
||||
"abab".contains("bb") //returns false
|
||||
"abab".contains("ab") //returns true
|
||||
int loc = "abab".indexOf("bb") //returns -1
|
||||
loc = "abab".indexOf("ab") //returns 0
|
||||
loc = "abab".indexOf("ab",loc+1) //returns 2
|
||||
27
Task/String-matching/JavaScript/string-matching.js
Normal file
27
Task/String-matching/JavaScript/string-matching.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
var stringA = "tacoloco"
|
||||
, stringB = "co"
|
||||
, q1, q2, q2multi, m
|
||||
, q2matches = []
|
||||
|
||||
// stringA starts with stringB
|
||||
q1 = stringA.substring(0, stringB.length) == stringB
|
||||
|
||||
// stringA contains stringB
|
||||
q2 = stringA.indexOf(stringB)
|
||||
|
||||
// multiple matches
|
||||
q2multi = new RegExp(stringB,'g')
|
||||
|
||||
while(m = q2multi.exec(stringA)){
|
||||
q2matches.push(m.index)
|
||||
}
|
||||
|
||||
// stringA ends with stringB
|
||||
q3 = stringA.substr(-stringB.length) == stringB
|
||||
|
||||
console.log("1: Does '"+stringA+"' start with '"+stringB+"'? " + ( q1 ? "Yes." : "No."))
|
||||
console.log("2: Is '"+stringB+"' contained in '"+stringA+"'? " + (~q2 ? "Yes, at index "+q2+"." : "No."))
|
||||
if (~q2 && q2matches.length > 1){
|
||||
console.log(" In fact, it happens "+q2matches.length+" times within '"+stringA+"', at index"+(q2matches.length > 1 ? "es" : "")+" "+q2matches.join(', ')+".")
|
||||
}
|
||||
console.log("3: Does '"+stringA+"' end with '"+stringB+"'? " + ( q3 ? "Yes." : "No."))
|
||||
9
Task/String-matching/Julia/string-matching.julia
Normal file
9
Task/String-matching/Julia/string-matching.julia
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
begins_with("abcd","ab") #returns true
|
||||
search("abcd","ab") #returns 1:2, indices range where string was found
|
||||
ends_with("abcd","zn") #returns false
|
||||
ismatch(r"ab","abcd") #returns true where 1st arg is regex string
|
||||
julia>for r in each_match(r"ab","abab")
|
||||
println(r.offset)
|
||||
end
|
||||
1
|
||||
3
|
||||
3
Task/String-matching/K/string-matching-1.k
Normal file
3
Task/String-matching/K/string-matching-1.k
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
startswith: {:[0<#p:_ss[x;y];~*p;0]}
|
||||
endswith: {0=(-#y)+(#x)-*_ss[x;y]}
|
||||
contains: {0<#_ss[x;y]}
|
||||
14
Task/String-matching/K/string-matching-2.k
Normal file
14
Task/String-matching/K/string-matching-2.k
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
startswith["abcd";"ab"]
|
||||
1
|
||||
startswith["abcd";"bc"]
|
||||
0
|
||||
endswith["abcd";"cd"]
|
||||
1
|
||||
endswith["abcd";"bc"]
|
||||
0
|
||||
contains["abcdef";"cde"]
|
||||
1
|
||||
contains["abcdef";"bdef"]
|
||||
0
|
||||
_ss["abcdabceabc";"abc"] / location of matches
|
||||
0 4 8
|
||||
20
Task/String-matching/Lang5/string-matching.lang5
Normal file
20
Task/String-matching/Lang5/string-matching.lang5
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
: 2array 2 compress ; : bi* '_ set dip _ execute ; : bi@ dup bi* ;
|
||||
: comb "" split ; : concat "" join ; : dip swap '_ set execute _ ;
|
||||
: first 0 extract swap drop ; : flip comb reverse concat ;
|
||||
|
||||
: contains
|
||||
swap 'comb bi@ length do # create a matrix.
|
||||
1 - "dup 1 1 compress rotate" dip dup 0 == if break then
|
||||
loop drop length compress swap drop
|
||||
"length 1 -" bi@ rot 0 0 "'2array dip" '2array bi* swap 2array slice reverse
|
||||
: concat.(*) concat ;
|
||||
'concat "'concat. apply" bi* eq 1 1 compress index collapse
|
||||
length if expand drop else drop 0 then ; # r: position.
|
||||
: end-with 'flip bi@ start-with ;
|
||||
: start-with 'comb bi@ length rot swap iota subscript 'concat bi@ eq ;
|
||||
|
||||
"rosettacode" "rosetta" start-with . # 1
|
||||
"rosettacode" "taco" contains . # 5
|
||||
"rosettacode" "ocat" contains . # 0
|
||||
"rosettacode" "edoc" end-with . # 0
|
||||
"rosettacode" "code" contains . # 7
|
||||
30
Task/String-matching/Liberty-BASIC/string-matching.liberty
Normal file
30
Task/String-matching/Liberty-BASIC/string-matching.liberty
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
'1---Determining if the first string starts with second string
|
||||
st1$="first string"
|
||||
st2$="first"
|
||||
if left$(st1$,len(st2$))=st2$ then
|
||||
print "First string starts with second string."
|
||||
end if
|
||||
|
||||
'2---Determining if the first string contains the second string at any location
|
||||
'2.1---Print the location of the match for part 2
|
||||
st1$="Mississippi"
|
||||
st2$="i"
|
||||
if instr(st1$,st2$) then
|
||||
print "First string contains second string."
|
||||
print "Second string is at location ";instr(st1$,st2$)
|
||||
end if
|
||||
|
||||
'2.2---Handle multiple occurrences of a string for part 2.
|
||||
pos=instr(st1$,st2$)
|
||||
while pos
|
||||
count=count+1
|
||||
pos=instr(st1$,st2$,pos+len(st2$))
|
||||
wend
|
||||
print "Number of times second string appears in first string is ";count
|
||||
|
||||
'3---Determining if the first string ends with the second string
|
||||
st1$="first string"
|
||||
st2$="string"
|
||||
if right$(st1$,len(st2$))=st2$ then
|
||||
print "First string ends with second string."
|
||||
end if
|
||||
17
Task/String-matching/Logo/string-matching.logo
Normal file
17
Task/String-matching/Logo/string-matching.logo
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
to starts.with? :sub :thing
|
||||
if empty? :sub [output "true]
|
||||
if empty? :thing [output "false]
|
||||
if not equal? first :sub first :thing [output "false]
|
||||
output starts.with? butfirst :sub butfirst :thing
|
||||
end
|
||||
|
||||
to ends.with? :sub :thing
|
||||
if empty? :sub [output "true]
|
||||
if empty? :thing [output "false]
|
||||
if not equal? last :sub last :thing [output "false]
|
||||
output ends.with? butlast :sub butlast :thing
|
||||
end
|
||||
|
||||
show starts.with? "dog "doghouse ; true
|
||||
show ends.with? "house "doghouse ; true
|
||||
show substring? "gho "doghouse ; true (built-in)
|
||||
13
Task/String-matching/Lua/string-matching.lua
Normal file
13
Task/String-matching/Lua/string-matching.lua
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
s1 = "string"
|
||||
s2 = "str"
|
||||
s3 = "ing"
|
||||
s4 = "xyz"
|
||||
|
||||
print( "s1 starts with s2: ", string.find( s1, s2 ) == 1 )
|
||||
print( "s1 starts with s3: ", string.find( s1, s3 ) == 1, "\n" )
|
||||
|
||||
print( "s1 contains s3: ", string.find( s1, s3 ) ~= nil )
|
||||
print( "s1 contains s3: ", string.find( s1, s4 ) ~= nil, "\n" )
|
||||
|
||||
print( "s1 ends with s2: ", select( 2, string.find( s1, s2 ) ) == string.len( s1 ) )
|
||||
print( "s1 ends with s3: ", select( 2, string.find( s1, s3 ) ) == string.len( s1 ) )
|
||||
11
Task/String-matching/MATLAB/string-matching.m
Normal file
11
Task/String-matching/MATLAB/string-matching.m
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
% 1. Determining if the first string starts with second string
|
||||
strcmp(str1,str2,length(str2))
|
||||
% 2. Determining if the first string contains the second string at any location
|
||||
~isempty(strfind(s1,s2))
|
||||
% 3. Determining if the first string ends with the second string
|
||||
( (length(str1)>=length(str2)) && strcmp(str1(end+[1-length(str2):0]),str2) )
|
||||
|
||||
% 1. Print the location of the match for part 2
|
||||
disp(strfind(s1,s2))
|
||||
% 2. Handle multiple occurrences of a string for part 2.
|
||||
ix = strfind(s1,s2); % ix is a vector containing the starting positions of s2 within s1
|
||||
31
Task/String-matching/Maple/string-matching.maple
Normal file
31
Task/String-matching/Maple/string-matching.maple
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
> with( StringTools ): # bind package exports at the top-level
|
||||
> s := "dzrIemaWWIMidXYZwGiqkOOn":
|
||||
> s[1..4]; # pick a prefix
|
||||
"dzrI"
|
||||
|
||||
> IsPrefix( s[ 1 .. 4 ], s ); # check it
|
||||
true
|
||||
|
||||
> s[ -4 .. -1 ]; # pick a suffix
|
||||
"kOOn"
|
||||
|
||||
> IsSuffix( s[ -4 .. -1 ], s ); # check it
|
||||
true
|
||||
|
||||
> p := Search( "XYZ", s ); # find a substring
|
||||
p := 14
|
||||
|
||||
> s[ p .. p + 2 ]; # check
|
||||
"XYZ"
|
||||
|
||||
> SearchAll( [ "WWI", "XYZ" ], s ); # search for multiple patterns
|
||||
[8, 1], [14, 2]
|
||||
|
||||
> to 3 do s := cat( s, s ) end: length( s ); # build a longer string by repeated doubling
|
||||
192
|
||||
|
||||
> p := SearchAll( "XYZ", s ); # find all occurrences
|
||||
p := 14, 38, 62, 86, 110, 134, 158, 182
|
||||
|
||||
> {seq}( s[ i .. i + 2 ], i = p ); # check them
|
||||
{"XYZ"}
|
||||
5
Task/String-matching/Mathematica/string-matching.math
Normal file
5
Task/String-matching/Mathematica/string-matching.math
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
StartWith[x_, y_] := MemberQ[Flatten[StringPosition[x, y]], 1]
|
||||
EndWith[x_, y_] := MemberQ[Flatten[StringPosition[x, y]], StringLength[x]]
|
||||
StartWith["XYZaaabXYZaaaaXYZXYZ", "XYZ"]
|
||||
EndWith["XYZaaabXYZaaaaXYZXYZ", "XYZ"]
|
||||
StringPosition["XYZaaabXYZaaaaXYZXYZ", "XYZ"]
|
||||
48
Task/String-matching/NetRexx/string-matching.netrexx
Normal file
48
Task/String-matching/NetRexx/string-matching.netrexx
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols
|
||||
|
||||
lipsum = ''
|
||||
x_ = 0
|
||||
x_ = x_ + 1; lipsum[0] = x_; lipsum[x_] = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
|
||||
x_ = x_ + 1; lipsum[0] = x_; lipsum[x_] = lipsum[1].reverse
|
||||
|
||||
srch = ''
|
||||
srch[1] = 'Lorem ipsum dolor sit amet'
|
||||
srch[2] = 'consectetur adipisicing elit'
|
||||
srch[3] = 'dolore magna aliqua.'
|
||||
|
||||
loop j_ = 1 to lipsum[0]
|
||||
x1 = lipsum[j_].pos(srch[1])
|
||||
x2 = lipsum[j_].pos(srch[2])
|
||||
x3 = lipsum[j_].lastpos(srch[3])
|
||||
|
||||
report(x1 = 1, lipsum[j_], srch[1], 'Test string starts with search string:', 'Test string does not start with search string:')
|
||||
report(x2 > 0, lipsum[j_], srch[2], 'Search string located in test string at position:' x2, 'Search string not found within test string:')
|
||||
report(x3 \= srch[3].length, lipsum[j_], srch[3], 'Test string ends with search string:', 'Test string does not start with search string:')
|
||||
end j_
|
||||
|
||||
many = ''
|
||||
x_ = 0
|
||||
x_ = x_ + 1; many[0] = x_; many[x_] = 'How many times does "many times" occur in this string?'
|
||||
x_ = x_ + 1; many[0] = x_; many[x_] = 'How often does "many times" occur in this string?'
|
||||
x_ = x_ + 1; many[0] = x_; many[x_] = 'How often does it occur in this string?'
|
||||
srch[4] = 'many times'
|
||||
|
||||
loop j_ = 1 to many[0]
|
||||
o_ = 0
|
||||
k_ = 0
|
||||
loop label dups until o_ = 0
|
||||
o_ = many[j_].pos(srch[4], o_ + 1)
|
||||
if o_ \= 0 then k_ = k_ + 1
|
||||
end dups
|
||||
report(k_ > 0, many[j_], srch[4], 'Number of times search string occurs:' k_, 'Search string not found')
|
||||
end j_
|
||||
|
||||
method report(state = boolean, ts, ss, testSuccess, testFailure) public static
|
||||
if state then say testSuccess
|
||||
else say testFailure
|
||||
say ' Test string:' ts
|
||||
say ' Search string:' ss
|
||||
say
|
||||
|
||||
return
|
||||
18
Task/String-matching/NewLISP/string-matching.newlisp
Normal file
18
Task/String-matching/NewLISP/string-matching.newlisp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(setq str "abcdefbcghi")
|
||||
|
||||
;; test if str starts with "ab"
|
||||
(starts-with str "ab")
|
||||
|
||||
;; find "bc" inside str
|
||||
(find "bc" str)
|
||||
|
||||
;; test if str ends with "ghi"
|
||||
(ends-with str "ghi")
|
||||
|
||||
;; find all positions of pattern inside str
|
||||
(define (find-all-pos pattern str)
|
||||
(let ((idx -1) (pos '()))
|
||||
(while (setq idx (find pattern str 0 (+ idx 1)))
|
||||
(push idx pos -1))))
|
||||
|
||||
(find-all-pos "bc" str)
|
||||
6
Task/String-matching/OCaml/string-matching-1.ocaml
Normal file
6
Task/String-matching/OCaml/string-matching-1.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
let match1 s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then false else
|
||||
let sub = String.sub s1 0 len2 in
|
||||
(sub = s2)
|
||||
10
Task/String-matching/OCaml/string-matching-2.ocaml
Normal file
10
Task/String-matching/OCaml/string-matching-2.ocaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
let match2 s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then false else
|
||||
let rec aux i =
|
||||
if i < 0 then false else
|
||||
let sub = String.sub s1 i len2 in
|
||||
if (sub = s2) then true else aux (pred i)
|
||||
in
|
||||
aux (len1 - len2)
|
||||
6
Task/String-matching/OCaml/string-matching-3.ocaml
Normal file
6
Task/String-matching/OCaml/string-matching-3.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
let match3 s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then false else
|
||||
let sub = String.sub s1 (len1 - len2) len2 in
|
||||
(sub = s2)
|
||||
10
Task/String-matching/OCaml/string-matching-4.ocaml
Normal file
10
Task/String-matching/OCaml/string-matching-4.ocaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
let match2_loc s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then (false, -1) else
|
||||
let rec aux i =
|
||||
if i < 0 then (false, -1) else
|
||||
let sub = String.sub s1 i len2 in
|
||||
if (sub = s2) then (true, i) else aux (pred i)
|
||||
in
|
||||
aux (len1 - len2)
|
||||
12
Task/String-matching/OCaml/string-matching-5.ocaml
Normal file
12
Task/String-matching/OCaml/string-matching-5.ocaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
let match2_num s1 s2 =
|
||||
let len1 = String.length s1
|
||||
and len2 = String.length s2 in
|
||||
if len1 < len2 then (false, 0) else
|
||||
let rec aux i n =
|
||||
if i < 0 then (n <> 0, n) else
|
||||
let sub = String.sub s1 i len2 in
|
||||
if (sub = s2)
|
||||
then aux (pred i) (succ n)
|
||||
else aux (pred i) (n)
|
||||
in
|
||||
aux (len1 - len2) 0
|
||||
13
Task/String-matching/Objeck/string-matching.objeck
Normal file
13
Task/String-matching/Objeck/string-matching.objeck
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
bundle Default {
|
||||
class Matching {
|
||||
function : Main(args : System.String[]) ~ Nil {
|
||||
"abcd"->StartsWith("ab")->PrintLine(); # returns true
|
||||
"abcd"->EndsWith("zn")->PrintLine(); # returns false
|
||||
("abab"->Find("bb") <> -1)->PrintLine(); # returns false
|
||||
("abab"->Find("ab") <> -1)->PrintLine(); # returns true
|
||||
loc := "abab"->Find("bb"); # returns -1
|
||||
loc := "abab"->Find("ab"); # returns 0
|
||||
loc := "abab"->Find("ab", loc + 1); # returns 2
|
||||
}
|
||||
}
|
||||
}
|
||||
5
Task/String-matching/Objective-C/string-matching.m
Normal file
5
Task/String-matching/Objective-C/string-matching.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[@"abcd" hasPrefix:@"ab"] //returns true
|
||||
[@"abcd" hasSuffix:@"zn"] //returns false
|
||||
int loc = [@"abab" rangeOfString:@"bb"].location //returns -1
|
||||
loc = [@"abab" rangeOfString:@"ab"].location //returns 0
|
||||
loc = [@"abab" rangeOfString:@"ab" options:0 range:NSMakeRange(loc+1, [@"abab" length]-(loc+1))].location //returns 2
|
||||
35
Task/String-matching/OxygenBasic/string-matching.oxy
Normal file
35
Task/String-matching/OxygenBasic/string-matching.oxy
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
string s="sdfkjhgsdfkdfgkbopefioqwurti487sdfkrglkjfs9wrtgjglsdfkdkjcnmmb.,msfjflkjsdfk"
|
||||
|
||||
string f="sdfk"
|
||||
|
||||
string cr=chr(13)+chr(10),tab=chr(9)
|
||||
|
||||
string pr="FIND STRING LOCATIONS" cr cr
|
||||
|
||||
sys a=0, b=1, count=0, ls=len(s), lf=len(f)
|
||||
|
||||
do
|
||||
a=instr b,s,f
|
||||
if a=0 then exit do
|
||||
count++
|
||||
if a=1 then pr+="Begins with keyword" cr
|
||||
pr+=count tab a cr
|
||||
if a=ls-lf+1 then pr+="Ends with keyword at " a cr
|
||||
b=a+1
|
||||
end do
|
||||
|
||||
pr+=cr "Total matches: " count cr
|
||||
|
||||
print pr
|
||||
|
||||
'FIND STRING LOCATIONS
|
||||
'
|
||||
'Begins with keyword
|
||||
'1 1
|
||||
'2 8
|
||||
'3 32
|
||||
'4 51
|
||||
'5 73
|
||||
'Ends with keyword at 73
|
||||
'
|
||||
'Total matches: 5
|
||||
27
Task/String-matching/PARI-GP/string-matching.pari
Normal file
27
Task/String-matching/PARI-GP/string-matching.pari
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
startsWith(string, prefix)={
|
||||
string=Vec(string);
|
||||
prefix=Vec(prefix);
|
||||
if(#prefix > #string, return(0));
|
||||
for(i=1,#prefix,if(prefix[i]!=string[i], return(0)));
|
||||
1
|
||||
};
|
||||
contains(string, inner)={
|
||||
my(good);
|
||||
string=Vec(string);
|
||||
inner=Vec(inner);
|
||||
for(i=0,#string-#inner,
|
||||
good=1;
|
||||
for(j=1,#inner,
|
||||
if(inner[j]!=string[i+j], good=0; break)
|
||||
);
|
||||
if(good, return(i+1))
|
||||
);
|
||||
0
|
||||
};
|
||||
endsWith(string, suffix)={
|
||||
string=Vec(string);
|
||||
suffix=Vec(suffix);
|
||||
if(#suffix > #string, return(0));
|
||||
for(i=1,#suffix,if(prefix[i]!=string[i+#string-#suffix], return(0)));
|
||||
1
|
||||
};
|
||||
59
Task/String-matching/PHP/string-matching-1.php
Normal file
59
Task/String-matching/PHP/string-matching-1.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/**********************************************************************************
|
||||
* This program gets needle and haystack from the caller (chm.html) (see below)
|
||||
* and checks for occurrences of the needle in the haystack
|
||||
* 02.05.2013 Walter Pachl
|
||||
* Comments or Suggestions welcome
|
||||
**********************************************************************************/
|
||||
$haystack = $_POST['haystack']; if ($haystack=='') {$haystack='no haystack given';}
|
||||
$needle = $_POST['needle']; if ($needle=='') {$needle='no needle given';}
|
||||
|
||||
function rexxpos($h,$n) {
|
||||
$pos = strpos($h,$n);
|
||||
if ($pos === false) { $pos=-1; }
|
||||
else { $pos=$pos+1; }
|
||||
return ($pos);
|
||||
}
|
||||
|
||||
$pos=rexxpos($haystack,$needle);
|
||||
$tx1 = "";
|
||||
if ($pos==-1){ $n=0; } // not found
|
||||
else { $n=1; } // found once (so far)
|
||||
// Special cases
|
||||
if ($pos==1){ $tx1="needle found to be the start of the haystack"; }
|
||||
if ($pos==strlen($haystack)-strlen($needle)+1)
|
||||
{ $tx1="needle found at end of haystack"; }
|
||||
|
||||
if ($n>0) { // look for other occurrences
|
||||
$pl=$pos; // list of positions
|
||||
$p=$pos; //
|
||||
$x="*************************************";
|
||||
$h=$haystack;
|
||||
while ($p>0) {
|
||||
$h=substr($x,0,$p).substr($h,$p);
|
||||
$p=rexxpos($h,$needle);
|
||||
if ( $p>0 ) { $n=$n+1; $pl=$pl.", ".$p; }
|
||||
}
|
||||
if ($n==1) { $txt="needle found once in haystack, position: $pl."; }
|
||||
else if ($n==2) { $txt="needle found twice in haystack, position(s): $pl."; }
|
||||
else { $txt="needle found $n times in haystack, position(s): $pl."; }
|
||||
}
|
||||
else { $txt="needle not found in haystack."; }
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
<title>Character Matching</title>
|
||||
<meta name="author" content="Walter Pachl">
|
||||
<meta name="date" content="02.05.2013">
|
||||
<style>
|
||||
p { font: 120% courier; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p><strong>Haystack: '<?php echo "$haystack" ?>'</strong></p>
|
||||
<p><strong>Needle: '<?php echo "$needle" ?>'</strong></p>
|
||||
<p><strong><?php echo "$txt" ?></strong></p>
|
||||
<!-- special message: -->
|
||||
<p style="color: red";><strong><?php echo "$tx1" ?></strong></p>
|
||||
</body>
|
||||
</html>
|
||||
41
Task/String-matching/PHP/string-matching-2.php
Normal file
41
Task/String-matching/PHP/string-matching-2.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
/************************************************************************
|
||||
* Here we prompt the user for a haystack and a needle
|
||||
* We then invoke program chmx.php
|
||||
* to check for occurrences of the needle in the haystack
|
||||
* 02.05.2013 Walter Pachl
|
||||
* Comments or Suggestions welcome
|
||||
************************************************************************/
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Character matching</title>
|
||||
</head>
|
||||
<body>
|
||||
<form id="test" name="test" method="post" action="chmx.php">
|
||||
<h1>Character matching</h1>
|
||||
<p>Given two strings, demonstrate the following 3 types of matchings:
|
||||
<ol style="margin-top:2; margin-bottom:2;">
|
||||
<li>Determining if the first string starts with second string
|
||||
<li>Determining if the first string contains the second string at any location
|
||||
<li>Determining if the first string ends with the second string
|
||||
</ol>
|
||||
<p>Optional requirements:
|
||||
<ol style="margin-top:2; margin-bottom:2;">
|
||||
<li>Print the location of the match(es) for part 2
|
||||
<li>Handle multiple occurrences of a string for part 2.
|
||||
</ol>
|
||||
<p style="margin-top:5; margin-bottom:3;">
|
||||
<font face="Courier"><strong>Haystack:</strong>
|
||||
<strong><input type="text" name="haystack" size="80"></strong></font></p>
|
||||
<p style="margin-top:5; margin-bottom:3;">
|
||||
<font face="Courier"><strong>Needle: </strong>
|
||||
<strong><input type="text" name="needle" size="80"></strong></font></p>
|
||||
<p>Press <input name="Submit" type="submit" class="erfolg" value="CHECK"/>
|
||||
to invoke chmx.php.</p>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
11
Task/String-matching/PL-I/string-matching-1.pli
Normal file
11
Task/String-matching/PL-I/string-matching-1.pli
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* Let s be one string, t be the other that might exist within s. */
|
||||
/* 8-1-2011 */
|
||||
k = index(s, t);
|
||||
if k = 0 then put skip edit (t, ' is nowhere in sight') (a);
|
||||
else if k = 1 then
|
||||
put skip edit (t, ' starts at the beginning of ', s) (a);
|
||||
else if k+length(t)-1 = length(s) then
|
||||
put skip edit (t, ' is at the end of ', s) (a);
|
||||
else put skip edit (t, ' is within ', s) (a);
|
||||
|
||||
if k > 0 then put skip edit (t, ' starts at position ', k) (a);
|
||||
20
Task/String-matching/PL-I/string-matching-2.pli
Normal file
20
Task/String-matching/PL-I/string-matching-2.pli
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* Handle multiple occurrences. */
|
||||
n = 1;
|
||||
do forever;
|
||||
k = index(s, t, n);
|
||||
if k = 0 then
|
||||
do;
|
||||
if n = 1 then put skip list (t, ' is nowhere in sight');
|
||||
stop;
|
||||
end;
|
||||
else if k = 1 then
|
||||
put skip edit ('<', t, '> starts at the beginning of ', s) (a);
|
||||
else if k+length(t)-1 = length(s) then
|
||||
put skip edit ('<', t, '> is at the end of ', s) (a);
|
||||
else put skip edit ('<', t, '> is within ', s) (a);
|
||||
n = k + length(t);
|
||||
|
||||
if k > 0 then
|
||||
put skip edit ('<', t, '> starts at position ', trim(k)) (a);
|
||||
else stop;
|
||||
end;
|
||||
34
Task/String-matching/Perl-6/string-matching.pl6
Normal file
34
Task/String-matching/Perl-6/string-matching.pl6
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
my @subs = (
|
||||
# Regex-based:
|
||||
sub R_contains ( Str $_, Str $s2 ) { ? m/ $s2 / },
|
||||
sub R_starts_with ( Str $_, Str $s2 ) { ? m/ ^ $s2 / },
|
||||
sub R_ends_with ( Str $_, Str $s2 ) { ? m/ $s2 $ / },
|
||||
|
||||
# Index-based:
|
||||
sub I_contains ( Str $_, Str $s2 ) { .index( $s2)\ .defined },
|
||||
sub I_starts_with ( Str $_, Str $s2 ) { my $m = .index( $s2); $m.defined and $m == 0 },
|
||||
sub I_ends_with ( Str $_, Str $s2 ) { my $m = .rindex($s2); $m.defined and $m == .chars - $s2.chars },
|
||||
|
||||
# Substr-based:
|
||||
sub S_starts_with ( Str $_, Str $s2 ) { .substr(0, $s2.chars) eq $s2 },
|
||||
sub S_ends_with ( Str $_, Str $s2 ) { .substr( -$s2.chars) eq $s2 },
|
||||
|
||||
# Optional tasks:
|
||||
sub R_find ( Str $_, Str $s2 ) { $/.from if /$s2/ },
|
||||
sub R_find_all ( Str $_, Str $s2 ) {
|
||||
my @p = .match: /$s2/, :g;
|
||||
@p».from if @p;
|
||||
},
|
||||
);
|
||||
|
||||
my $str1 = 'abcbcbcd';
|
||||
my @str2s = < ab bc cd zz >;
|
||||
|
||||
say "'$str1' vs:".fmt('%15s '), @str2s.fmt('%-15s');
|
||||
for [1, 4, 6], [2, 5, 7], [0, 3], [8, 9] {
|
||||
say;
|
||||
for @subs[.list] -> $sub {
|
||||
say "{$sub.name}:".fmt('%15s '),
|
||||
@str2s.map({ ~$sub.($str1, $_) }).fmt('%-15s');
|
||||
}
|
||||
}
|
||||
3
Task/String-matching/Perl/string-matching-1.pl
Normal file
3
Task/String-matching/Perl/string-matching-1.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$str1 =~ /^\Q$str2\E/ # true if $str1 starts with $str2
|
||||
$str1 =~ /\Q$str2\E/ # true if $str1 contains $str2
|
||||
$str1 =~ /\Q$str2\E$/ # true if $str1 ends with $str2
|
||||
3
Task/String-matching/Perl/string-matching-2.pl
Normal file
3
Task/String-matching/Perl/string-matching-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
index($str1, $str2) == 0 # true if $str1 starts with $str2
|
||||
index($str1, $str2) != -1 # true if $str1 contains $str2
|
||||
rindex($str1, $str2) == length($str1) - length($str2) # true if $str1 ends with $str2
|
||||
2
Task/String-matching/Perl/string-matching-3.pl
Normal file
2
Task/String-matching/Perl/string-matching-3.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
substr($str1, 0, length($str2)) eq $str2 # true if $str1 starts with $str2
|
||||
substr($str1, - length($str2)) eq $str2 # true if $str1 ends with $str2
|
||||
1
Task/String-matching/Perl/string-matching-4.pl
Normal file
1
Task/String-matching/Perl/string-matching-4.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
print $-[0], "\n" while $str1 =~ /\Q$str2\E/g; # using a regex
|
||||
1
Task/String-matching/Perl/string-matching-5.pl
Normal file
1
Task/String-matching/Perl/string-matching-5.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
my $i = -1; print $i, "\n" while ($i = index $str1, $str2, $i + 1) != -1; # using index
|
||||
24
Task/String-matching/PicoLisp/string-matching.l
Normal file
24
Task/String-matching/PicoLisp/string-matching.l
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
: (pre? "ab" "abcd")
|
||||
-> "abcd"
|
||||
: (pre? "xy" "abcd")
|
||||
-> NIL
|
||||
|
||||
: (sub? "bc" "abcd")
|
||||
-> "abcd"
|
||||
: (sub? "xy" "abcd")
|
||||
-> NIL
|
||||
|
||||
: (tail (chop "cd") (chop "abcd"))
|
||||
-> ("c" "d")
|
||||
: (tail (chop "xy") (chop "abcd"))
|
||||
-> NIL
|
||||
|
||||
|
||||
(de positions (Pat Str)
|
||||
(setq Pat (chop Pat))
|
||||
(make
|
||||
(for ((I . L) (chop Str) L (cdr L))
|
||||
(and (head Pat L) (link I)) ) ) )
|
||||
|
||||
: (positions "bc" "abcdabcd")
|
||||
-> (2 6)
|
||||
5
Task/String-matching/PowerShell/string-matching.psh
Normal file
5
Task/String-matching/PowerShell/string-matching.psh
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"spicywiener".StartsWith("spicy")
|
||||
"spicywiener".Contains("icy")
|
||||
"spicywiener".EndsWith("wiener")
|
||||
"spicywiener".IndexOf("icy")
|
||||
[regex]::Matches("spicywiener", "i").count
|
||||
15
Task/String-matching/PureBasic/string-matching-1.purebasic
Normal file
15
Task/String-matching/PureBasic/string-matching-1.purebasic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Procedure StartsWith(String1$, String2$)
|
||||
Protected Result
|
||||
If FindString(String1$, String2$, 1) =1 ; E.g Found in possition 1
|
||||
Result =CountString(String1$, String2$)
|
||||
EndIf
|
||||
ProcedureReturn Result
|
||||
EndProcedure
|
||||
|
||||
Procedure EndsWith(String1$, String2$)
|
||||
Protected Result, dl=Len(String1$)-Len(String2$)
|
||||
If dl>=0 And Right(String1$, Len(String2$))=String2$
|
||||
Result =CountString(String1$, String2$)
|
||||
EndIf
|
||||
ProcedureReturn Result
|
||||
EndProcedure
|
||||
11
Task/String-matching/PureBasic/string-matching-2.purebasic
Normal file
11
Task/String-matching/PureBasic/string-matching-2.purebasic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
If OpenConsole()
|
||||
PrintN(Str(StartsWith("Rosettacode", "Rosetta"))) ; = 1
|
||||
PrintN(Str(StartsWith("Rosettacode", "code"))) ; = 0
|
||||
PrintN(Str(StartsWith("eleutherodactylus cruralis", "e"))) ; = 3
|
||||
PrintN(Str(EndsWith ("Rosettacode", "Rosetta"))) ; = 0
|
||||
PrintN(Str(EndsWith ("Rosettacode", "code"))) ; = 1
|
||||
PrintN(Str(EndsWith ("Rosettacode", "e"))) ; = 2
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
42
Task/String-matching/PureBasic/string-matching-3.purebasic
Normal file
42
Task/String-matching/PureBasic/string-matching-3.purebasic
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
Procedure startsWith(string1$, string2$)
|
||||
;returns one if string1$ starts with string2$, otherwise returns zero
|
||||
If FindString(string1$, string2$, 1) = 1
|
||||
ProcedureReturn 1
|
||||
EndIf
|
||||
ProcedureReturn 0
|
||||
EndProcedure
|
||||
|
||||
Procedure contains(string1$, string2$, location = 0)
|
||||
;returns the location of the next occurrence of string2$ in string1$ starting from location,
|
||||
;or zero if no remaining occurrences of string2$ are found in string1$
|
||||
ProcedureReturn FindString(string1$, string2$, location + 1)
|
||||
EndProcedure
|
||||
|
||||
Procedure endsWith(string1$, string2$)
|
||||
;returns one if string1$ ends with string2$, otherwise returns zero
|
||||
Protected ls = Len(string2$)
|
||||
If Len(string1$) - ls >= 0 And Right(string1$, ls) = string2$
|
||||
ProcedureReturn 1
|
||||
EndIf
|
||||
ProcedureReturn 0
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
PrintN(Str(startsWith("RosettaCode", "Rosetta"))) ; = 1, true
|
||||
PrintN(Str(startsWith("RosettaCode", "Code"))) ; = 0, false
|
||||
|
||||
PrintN("")
|
||||
PrintN(Str(contains("RosettaCode", "luck"))) ; = 0, no occurrences
|
||||
Define location
|
||||
Repeat
|
||||
location = contains("eleutherodactylus cruralis", "e", location)
|
||||
PrintN(Str(location)) ;display each occurrence: 1, 3, 7, & 0 (no more occurrences)
|
||||
Until location = 0
|
||||
|
||||
PrintN("")
|
||||
PrintN(Str(endsWith ("RosettaCode", "Rosetta"))) ; = 0, false
|
||||
PrintN(Str(endsWith ("RosettaCode", "Code"))) ; = 1, true
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
7
Task/String-matching/Python/string-matching.py
Normal file
7
Task/String-matching/Python/string-matching.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
"abcd".startswith("ab") #returns True
|
||||
"abcd".endswith("zn") #returns False
|
||||
"bb" in "abab" #returns False
|
||||
"ab" in "abab" #returns True
|
||||
loc = "abab".find("bb") #returns -1
|
||||
loc = "abab".find("ab") #returns 0
|
||||
loc = "abab".find("ab",loc+1) #returns 2
|
||||
1
Task/String-matching/README
Normal file
1
Task/String-matching/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/String_matching
|
||||
33
Task/String-matching/REXX/string-matching.rexx
Normal file
33
Task/String-matching/REXX/string-matching.rexx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*REXX program demonstrates some basic character string testing. */
|
||||
parse arg a b /*obtain A and B from the C.L. */
|
||||
say 'string A = ' a /*display string A to terminal.*/
|
||||
say 'string B = ' b /* " " B " " */
|
||||
say
|
||||
if left(A,length(b))==b then say 'string A starts with string B'
|
||||
else say "string A doesn't start with string B"
|
||||
say
|
||||
/*another method, however a wee bit obtuse. */
|
||||
/*¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
|
||||
if compare(a,b)==length(b) then say 'string A starts with string B'
|
||||
else say "string A doesn't start with string B"
|
||||
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬*/
|
||||
/* [↑] above is a big comment. */
|
||||
p=pos(b,a)
|
||||
if p==0 then say "string A doesn't contain string B"
|
||||
else say 'string A contains string B (starting in position' p")"
|
||||
say
|
||||
if right(A,length(b))==b then say 'string A ends with string B'
|
||||
else say "string A doesn't end with string B"
|
||||
say
|
||||
Ps=''; p=0; do until p==0
|
||||
p=pos(b, a, p+1)
|
||||
if p\==0 then Ps = Ps',' p
|
||||
end /*until ···*/
|
||||
Ps=space(strip(Ps, 'L', ","))
|
||||
times=words(Ps)
|
||||
if times==0 then say "string A doesn't contain string B"
|
||||
else say 'string A contains string B ',
|
||||
times 'time'left('s',times>1),
|
||||
"(at position"left('s', times>1) Ps')'
|
||||
|
||||
/*stick a fork in it, we're done.*/
|
||||
6
Task/String-matching/Racket/string-matching.rkt
Normal file
6
Task/String-matching/Racket/string-matching.rkt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#lang racket
|
||||
(require srfi/13)
|
||||
(string-prefix? "ab" "abcd")
|
||||
(string-suffix? "cd" "abcd")
|
||||
(string-contains "abab" "bb")
|
||||
(string-contains "abab" "ba")
|
||||
14
Task/String-matching/Retro/string-matching.retro
Normal file
14
Task/String-matching/Retro/string-matching.retro
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
: startsWith? ( $1 $2 - f )
|
||||
withLength &swap dip 0 swap ^strings'getSubset compare ;
|
||||
|
||||
"abcdefghijkl" "abcde" startsWith?
|
||||
"abcdefghijkl" "bcd" startsWith?
|
||||
|
||||
"abcdefghijkl" "bcd" ^strings'search
|
||||
"abcdefghijkl" "zmq" ^strings'search
|
||||
|
||||
: endsWith? ( $1 $2 - f )
|
||||
swap withLength + over getLength - compare ;
|
||||
|
||||
"abcdefghijkl" "ijkl" endsWith?
|
||||
"abcdefghijkl" "abc" endsWith?
|
||||
8
Task/String-matching/Ruby/string-matching.rb
Normal file
8
Task/String-matching/Ruby/string-matching.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
p 'abcd'.start_with?('ab') #returns true
|
||||
p 'abcd'.end_with?('ab') #returns false
|
||||
p 'abab'.include?('bb') #returns false
|
||||
p 'abab'.include?('ab') #returns true
|
||||
p 'abab'.index('bb') #returns nil
|
||||
p 'abab'.index('ab') #returns 0
|
||||
p 'abab'.index('ab', 1) #returns 2
|
||||
p 'abab'.rindex('ab') #returns 2
|
||||
28
Task/String-matching/Run-BASIC/string-matching.run
Normal file
28
Task/String-matching/Run-BASIC/string-matching.run
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
s1$ = "abc def ghi klmnop"
|
||||
s2$ = "abc" ' begins with
|
||||
s3$ = "ef" ' is in the string
|
||||
s4$ = "nop" ' ends with
|
||||
|
||||
sn2$ = "abcx" ' not begins with
|
||||
sn3$ = "efx" ' not in the string
|
||||
sn4$ = "nopx" ' not ends with
|
||||
|
||||
if left$(s1$,len(s2$)) <> s2$ then a$ = "Not "
|
||||
print "String:";s1$;" does ";a$;"begin with:";s2$
|
||||
|
||||
if instr(s1$,s3$) = 0 then a$ = "Not "
|
||||
print "String:";s1$;" does ";a$;"contain:";s3$
|
||||
|
||||
if mid$(s1$,len(s1$) + 1 - len(s4$),len(s4$)) <> s4$ then a$ = "Not "
|
||||
print "String:";s1$;" does ";a$;"end with:";s4$
|
||||
|
||||
' ----------- not -----------------------------
|
||||
print
|
||||
if left$(s1$,len(sn2$)) <> sn2$ then a$ = "Not "
|
||||
print "String:";s1$;" does ";a$;"begin with:";sn2$
|
||||
|
||||
if instr(s1$,sn3$) = 0 then a$ = "Not "
|
||||
print "String:";s1$;" does ";a$;"contain:";sn3$
|
||||
|
||||
if mid$(s1$,len(s1$) + 1 - len(sn4$),len(sn4$)) <> sn4$ then a$ = "Not "
|
||||
print "String:";s1$;" does ";a$;"end with:";sn4$
|
||||
8
Task/String-matching/Scala/string-matching.scala
Normal file
8
Task/String-matching/Scala/string-matching.scala
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
"abcd".startsWith("ab") //returns true
|
||||
"abcd".endsWith("zn") //returns false
|
||||
"abab".contains("bb") //returns false
|
||||
"abab".contains("ab") //returns true
|
||||
|
||||
var loc="abab".indexOf("bb") //returns -1
|
||||
loc = "abab".indexOf("ab") //returns 0
|
||||
loc = "abab".indexOf("ab", loc+1) //returns 2
|
||||
16
Task/String-matching/Seed7/string-matching.seed7
Normal file
16
Task/String-matching/Seed7/string-matching.seed7
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: position is 0;
|
||||
begin
|
||||
writeln(startsWith("abcd", "ab")); # write TRUE
|
||||
writeln(endsWith("abcd", "zn")); # write FALSE
|
||||
writeln(pos("abab", "bb") <> 0); # write FALSE
|
||||
writeln(pos("abab", "ab") <> 0); # write TRUE
|
||||
writeln(pos("abab", "bb")); # write 0
|
||||
position := pos("abab", "ab");
|
||||
writeln(position); # position is 1
|
||||
position := pos("abab", "ab", succ(position));
|
||||
writeln(position); # position is 3
|
||||
end func;
|
||||
5
Task/String-matching/Smalltalk/string-matching.st
Normal file
5
Task/String-matching/Smalltalk/string-matching.st
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
a startsWith: b
|
||||
a includesSubCollection: b
|
||||
a endsWith: b
|
||||
a indexOfSubCollection: b
|
||||
a indexOfSubCollection: b startingAt: pos
|
||||
7
Task/String-matching/Standard-ML/string-matching.ml
Normal file
7
Task/String-matching/Standard-ML/string-matching.ml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
String.isPrefix "ab" "abcd"; (* returns true *)
|
||||
String.isSuffix "zn" "abcd"; (* returns false *)
|
||||
String.isSubstring "bb" "abab"; (* returns false *)
|
||||
String.isSubstring "ab" "abab"; (* returns true *)
|
||||
#2 (Substring.base (#2 (Substring.position "bb" (Substring.full "abab")))); (* returns 4 *)
|
||||
val loc = #2 (Substring.base (#2 (Substring.position "ab" (Substring.full "abab")))); (* returns 0 *)
|
||||
val loc' = #2 (Substring.base (#2 (Substring.position "ab" (Substring.extract ("abab", loc+1, NONE))))); (* returns 2 *)
|
||||
23
Task/String-matching/TUSCRIPT/string-matching.tu
Normal file
23
Task/String-matching/TUSCRIPT/string-matching.tu
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
$$ MODE TUSCRIPT
|
||||
ASK "string1", string1=""
|
||||
ASK "string2", string2=""
|
||||
|
||||
IF (string1.sw.string2) THEN
|
||||
PRINT string1," starts with ",string2
|
||||
ELSE
|
||||
PRINT string1," not starts with ",string2
|
||||
ENDIF
|
||||
SET beg=STRING (string1,string2,0,0,0,end)
|
||||
IF (beg!=0) THEN
|
||||
PRINT string1," contains ",string2
|
||||
PRINT " starting in position ",beg
|
||||
PRINT " ending in position ",end
|
||||
ELSE
|
||||
PRINT string1," not contains ",string2
|
||||
ENDIF
|
||||
|
||||
IF (string1.ew.string2) THEN
|
||||
PRINT string1," ends with ",string2
|
||||
ELSE
|
||||
PRINT string1," not ends with ",string2
|
||||
ENDIF
|
||||
18
Task/String-matching/TXR/string-matching-1.txr
Normal file
18
Task/String-matching/TXR/string-matching-1.txr
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@(do
|
||||
(tree-case *args*
|
||||
((big small)
|
||||
(cond
|
||||
((< (length big) (length small))
|
||||
(put-line `@big is shorter than @small`))
|
||||
((str= big small)
|
||||
(put-line `@big and @small are equal`))
|
||||
((match-str big small)
|
||||
(put-line `@small is a prefix of @big`))
|
||||
((match-str big small -1)
|
||||
(put-line `@small is a suffix of @big`))
|
||||
(t (let ((pos (search-str big small)))
|
||||
(if pos
|
||||
(put-line `@small occurs in @big at position @pos`)
|
||||
(put-line `@small does not occur in @big`))))))
|
||||
(otherwise
|
||||
(put-line `usage: @(ldiff *full-args* *args*) <bigstring> <smallstring>`))))
|
||||
26
Task/String-matching/TXR/string-matching-2.txr
Normal file
26
Task/String-matching/TXR/string-matching-2.txr
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
@line
|
||||
@(cases)
|
||||
@ line
|
||||
@ (output)
|
||||
second line is the same as first line
|
||||
@ (end)
|
||||
@(or)
|
||||
@ (skip)@line
|
||||
@ (output)
|
||||
first line is a suffix of the second line
|
||||
@ (end)
|
||||
@(or)
|
||||
@ line@(skip)
|
||||
@ (output)
|
||||
first line is a suffix of the second line
|
||||
@ (end)
|
||||
@(or)
|
||||
@ prefix@line@(skip)
|
||||
@ (output)
|
||||
first line is embedded in the second line at position @(length prefix)
|
||||
@ (end)
|
||||
@(or)
|
||||
@ (output)
|
||||
first line is not found in the second line
|
||||
@ (end)
|
||||
@(end)
|
||||
3
Task/String-matching/Tcl/string-matching-1.tcl
Normal file
3
Task/String-matching/Tcl/string-matching-1.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
set isPrefix [string equal -length [string length $needle] $haystack $needle]
|
||||
set isContained [expr {[string first $needle $haystack] >= 0}]
|
||||
set isSuffix [string equal $needle [string range $haystack end-[expr {[string length $needle]-1}] end]]
|
||||
3
Task/String-matching/Tcl/string-matching-2.tcl
Normal file
3
Task/String-matching/Tcl/string-matching-2.tcl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
set isPrefix [string match $needle* $haystack]
|
||||
set isContained [string match *$needle* $haystack]
|
||||
set isSuffix [string match *$needle $haystack]
|
||||
1
Task/String-matching/Tcl/string-matching-3.tcl
Normal file
1
Task/String-matching/Tcl/string-matching-3.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
set isContained [regexp ***=$needle $haystack]
|
||||
12
Task/String-matching/Tcl/string-matching-4.tcl
Normal file
12
Task/String-matching/Tcl/string-matching-4.tcl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
set matchLocations [regexp -indices -all -inline ***=$needle $haystack]
|
||||
# Each match location is a pair, being the index into the string where the needle started
|
||||
# to match and the index where the needle finished matching
|
||||
|
||||
set isContained [expr {[llength $matchLocations] > 0}]
|
||||
set isPrefix [expr {[lindex $matchLocations 0 0] == 0}]
|
||||
set isSuffix [expr {[lindex $matchLocations end 1] == [string length $haystack]-1}]
|
||||
set firstMatchStart [lindex $matchLocations 0 0]
|
||||
puts "Found \"$needle\" in \"$haystack\" at $firstMatchStart"
|
||||
foreach location $matchLocations {
|
||||
puts "needle matched at index [lindex $location 0]"
|
||||
}
|
||||
42
Task/String-matching/XPL0/string-matching.xpl0
Normal file
42
Task/String-matching/XPL0/string-matching.xpl0
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
include c:\cxpl\codes; \intrinsic 'code' declarations
|
||||
string 0; \use zero-terminated strings
|
||||
|
||||
func StrLen(A); \Return number of characters in a string
|
||||
char A;
|
||||
int I;
|
||||
for I:= 0 to -1>>1-1 do
|
||||
if A(I) = 0 then return I;
|
||||
|
||||
func StrFind(A, B); \Search for string B in string A
|
||||
\Returns index of first occurrence of string B in A, or -1 if B is not found
|
||||
char A, B; \strings to be compared
|
||||
int LA, LB, I, J;
|
||||
[LA:= StrLen(A);
|
||||
LB:= StrLen(B);
|
||||
for I:= 0 to LA-LB do
|
||||
[for J:= 0 to LB-1 do
|
||||
if B(J) # A(J+I) then J:= LB+1;
|
||||
if J = LB then return I; \found
|
||||
];
|
||||
return -1;
|
||||
];
|
||||
|
||||
char Str; int I, J;
|
||||
[Str:= "pack my box with";
|
||||
\ 0123456789012345
|
||||
Text(0, if StrFind(Str, "pack") = 0 then "yes" else "no"); CrLf(0); \1.
|
||||
Text(0, if StrFind(Str, "ack") = 0 then "yes" else "no"); CrLf(0);
|
||||
I:= StrFind(Str, "x w");
|
||||
Text(0, if I >= 0 then "yes" else "no"); \2.
|
||||
Text(0, ", at offset "); IntOut(0, I); CrLf(0);
|
||||
I:= 0; J:= 0; \offsets of space characters
|
||||
loop [I:= StrFind(Str+J, " ");
|
||||
if I < 0 then quit;
|
||||
IntOut(0, I+J); ChOut(0, ^ );
|
||||
J:= J+I+1;
|
||||
];
|
||||
CrLf(0);
|
||||
Text(0, if StrFind(Str, "X w") >= 0 then "yes" else "no"); CrLf(0);
|
||||
Text(0, if StrFind(Str, "with") = StrLen(Str)-StrLen("with") then "yes" else "no"); CrLf(0); \3.
|
||||
Text(0, if StrFind(Str, "x w" ) = StrLen(Str)-StrLen("x w" ) then "yes" else "no"); CrLf(0);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue