Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
20
Task/String-matching/Ada/string-matching.adb
Normal file
20
Task/String-matching/Ada/string-matching.adb
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;
|
||||
29
Task/String-matching/AutoIt/string-matching.au3
Normal file
29
Task/String-matching/AutoIt/string-matching.au3
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
|
||||
70
Task/String-matching/Batch-File/string-matching-1.bat
Normal file
70
Task/String-matching/Batch-File/string-matching-1.bat
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
::NOTE #1: This implementation might crash, or might not work properly if
|
||||
::you put some of the CMD special characters (ex. %,!, etc) inside the strings.
|
||||
::
|
||||
::NOTE #2: The comparisons here are case-SENSITIVE.
|
||||
::NOTE #3: Spaces in strings are considered.
|
||||
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
::The main things...
|
||||
set "str1=qwertyuiop"
|
||||
set "str2=qwerty"
|
||||
call :str2_lngth
|
||||
call :matchbegin
|
||||
|
||||
set "str1=qweiuoiocghiioyiocxiisfguiioiuygvd"
|
||||
set "str2=io"
|
||||
call :str2_lngth
|
||||
call :matchcontain
|
||||
|
||||
set "str1=blablabla"
|
||||
set "str2=bbla"
|
||||
call :str2_lngth
|
||||
call :matchend
|
||||
|
||||
echo.
|
||||
pause
|
||||
exit /b 0
|
||||
::/The main things.
|
||||
|
||||
::The functions...
|
||||
:matchbegin
|
||||
echo.
|
||||
if "!str1:~0,%length%!"=="!str2!" (
|
||||
echo "%str1%" begins with "%str2%".
|
||||
) else (
|
||||
echo "%str1%" does not begin with "%str2%".
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:matchcontain
|
||||
echo.
|
||||
set curr=0&set exist=0
|
||||
:scanchrloop
|
||||
if "!str1:~%curr%,%length%!"=="" (
|
||||
if !exist!==0 echo "%str1%" does not contain "%str2%".
|
||||
goto :EOF
|
||||
)
|
||||
if "!str1:~%curr%,%length%!"=="!str2!" (
|
||||
echo "%str1%" contains "%str2%". ^(in Position %curr%^)
|
||||
set exist=1
|
||||
)
|
||||
set /a curr+=1&goto scanchrloop
|
||||
|
||||
:matchend
|
||||
echo.
|
||||
if "!str1:~-%length%!"=="!str2!" (
|
||||
echo "%str1%" ends with "%str2%".
|
||||
) else (
|
||||
echo "%str1%" does not end with "%str2%".
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:str2_lngth
|
||||
set length=0
|
||||
:loop
|
||||
if "!str2:~%length%,1!"=="" goto :EOF
|
||||
set /a length+=1
|
||||
goto loop
|
||||
::/The functions.
|
||||
12
Task/String-matching/Batch-File/string-matching-2.bat
Normal file
12
Task/String-matching/Batch-File/string-matching-2.bat
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
@echo off
|
||||
setlocal enableextensions enabledelayedexpansion
|
||||
set /p a=
|
||||
set /p b=
|
||||
set /a i=0
|
||||
for %%i in (^^%b% %b% %b%$) do (
|
||||
echo/%a% | findstr /rc:"%%i" >nul 2>nul && set contain.!i!=Yes|| set contain.!i!=No
|
||||
set /a i+=1
|
||||
)
|
||||
echo "%a%" starts with "%b%": %contain.0%
|
||||
echo "%a%" ends with "%b%": %contain.1%
|
||||
echo "%a%" contains "%b%": %contain.2%
|
||||
15
Task/String-matching/Batch-File/string-matching-3.bat
Normal file
15
Task/String-matching/Batch-File/string-matching-3.bat
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
@echo off
|
||||
setlocal enableextensions enabledelayedexpansion
|
||||
set /p a=
|
||||
set /p b=
|
||||
set "macroset=(set contain.M=Yes) else (set contain.M=No)"
|
||||
:: https://ss64.com/nt/syntax-replace.html
|
||||
call set "t=%%a:%b%=#%%"
|
||||
if "#"=="%t:~0,1%" %macroset:M=0%
|
||||
call set "t=%%a:*%b%=%%"
|
||||
if ""=="%t%" %macroset:M=1%
|
||||
call set "t=%%a:%b%=%%"
|
||||
if not "%a%"=="%t%" %macroset:M=2%
|
||||
echo "%a%" starts with "%b%": %contain.0%
|
||||
echo "%a%" ends with "%b%": %contain.1%
|
||||
echo "%a%" contains "%b%": %contain.2%
|
||||
16
Task/String-matching/Batch-File/string-matching-4.bat
Normal file
16
Task/String-matching/Batch-File/string-matching-4.bat
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
@echo off
|
||||
setlocal enableextensions enabledelayedexpansion
|
||||
set /p a=
|
||||
set /p b=
|
||||
call :getres startsWith.bat "%a%" "%b%"
|
||||
echo "%a%" starts with "%b%": %result%
|
||||
call :getres endsWith.bat "%a%" "%b%"
|
||||
echo "%a%" ends with "%b%": %result%
|
||||
set idx=
|
||||
call indexOf.bat "%a%" "%b%" idx
|
||||
echo "%a%" contains "%b%" at: %idx%
|
||||
goto:eof
|
||||
|
||||
:getres
|
||||
call %1 %2 %3 && set result=No||set result=Yes
|
||||
goto:eof
|
||||
12
Task/String-matching/Crystal/string-matching.cr
Normal file
12
Task/String-matching/Crystal/string-matching.cr
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
p "banana".starts_with? "ba" #=> true
|
||||
p "banana".starts_with? "na" #=> false
|
||||
|
||||
p "banana".includes? "an" #=> true
|
||||
p "banana".includes? "xa" #=> false
|
||||
|
||||
p "banana".ends_with? "na" #=> true
|
||||
p "banana".ends_with? "ba" #=> false
|
||||
|
||||
p "banana".index "na" #=> 2
|
||||
p "banana".index "na", 3 #=> 4
|
||||
p "banana".index "na", 5 #=> nil
|
||||
14
Task/String-matching/Emacs-Lisp/string-matching.el
Normal file
14
Task/String-matching/Emacs-Lisp/string-matching.el
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(defun string-contains (needle haystack)
|
||||
(string-match (regexp-quote needle) haystack))
|
||||
|
||||
(string-prefix-p "before" "before center after") ;=> t
|
||||
(string-contains "before" "before center after") ;=> 0
|
||||
(string-suffix-p "before" "before center after") ;=> nil
|
||||
|
||||
(string-prefix-p "center" "before center after") ;=> nil
|
||||
(string-contains "center" "before center after") ;=> 7
|
||||
(string-suffix-p "center" "before center after") ;=> nil
|
||||
|
||||
(string-prefix-p "after" "before center after") ;=> nil
|
||||
(string-contains "after" "before center after") ;=> 14
|
||||
(string-suffix-p "after" "before center after") ;=> t
|
||||
30
Task/String-matching/Euphoria/string-matching.eu
Normal file
30
Task/String-matching/Euphoria/string-matching.eu
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
|
||||
11
Task/String-matching/Factor/string-matching.factor
Normal file
11
Task/String-matching/Factor/string-matching.factor
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* Does cheesecake start with cheese? */
|
||||
"cheesecake" "cheese" head? ! t
|
||||
/* Does cheesecake contain sec at any location? */
|
||||
"sec" "cheesecake" subseq? ! t
|
||||
/* Does cheesecake end with cake? */
|
||||
"cheesecake" "cake" tail? ! t
|
||||
/* Where in cheesecake is the leftmost sec? */
|
||||
"sec" "cheesecake" subseq-start ! 4
|
||||
/* Where in Mississippi are all occurrences of iss? */
|
||||
USE: regexp
|
||||
"Mississippi" "iss" <regexp> all-matching-slices [ from>> ] map ! { 1 4 }
|
||||
20
Task/String-matching/Frink/string-matching.frink
Normal file
20
Task/String-matching/Frink/string-matching.frink
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
s1 = "string"
|
||||
s2 = "str"
|
||||
s3 = "ing"
|
||||
s4 = "xyz"
|
||||
|
||||
println["$s1 starts with $s2: " + startsWith[s1, s2]]
|
||||
println["$s1 starts with $s4: " + startsWith[s1, s4]]
|
||||
println["$s1 ends with $s3: " + endsWith[s1, s3]]
|
||||
println["$s1 ends with $s4: " + endsWith[s1, s4]]
|
||||
position[s1, s2]
|
||||
position[s1, s4]
|
||||
|
||||
position[s1, s2] :=
|
||||
{
|
||||
pos = indexOf[s1,s2]
|
||||
if pos == -1
|
||||
println["$s1 does not contain $s2"]
|
||||
else
|
||||
println["$s1 contains $s2 at position $pos"]
|
||||
}
|
||||
15
Task/String-matching/M2000-Interpreter/string-matching.m2000
Normal file
15
Task/String-matching/M2000-Interpreter/string-matching.m2000
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Module StringMatch {
|
||||
A$="Hello World"
|
||||
Print A$ ~ "Hello*"
|
||||
Print A$ ~ "*llo*"
|
||||
p=Instr(A$, "llo")
|
||||
Print p=3
|
||||
\\ Handle multiple occurance for "o"
|
||||
p=Instr(A$, "o")
|
||||
While p > 0 {
|
||||
Print "position:";p;{ for "o"}
|
||||
p=Instr(A$, "o", p+1)
|
||||
}
|
||||
Print A$ ~ "*orld"
|
||||
}
|
||||
StringMatch
|
||||
86
Task/String-matching/OCaml/string-matching.ml
Normal file
86
Task/String-matching/OCaml/string-matching.ml
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
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)
|
||||
|
||||
(* testing in the top-level:
|
||||
|
||||
# match1 "Hello" "Hello World!" ;;
|
||||
- : bool = false
|
||||
# match1 "Hello World!" "Hello" ;;
|
||||
- : bool = true
|
||||
*)
|
||||
|
||||
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)
|
||||
|
||||
(*
|
||||
# match2 "It's raining, Hello World!" "umbrella" ;;
|
||||
- : bool = false
|
||||
# match2 "It's raining, Hello World!" "Hello" ;;
|
||||
- : bool = true
|
||||
*)
|
||||
|
||||
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)
|
||||
|
||||
(*
|
||||
# match3 "Hello World" "Hello" ;;
|
||||
- : bool = false
|
||||
# match3 "Hello World" "World" ;;
|
||||
- : bool = true
|
||||
*)
|
||||
|
||||
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)
|
||||
|
||||
(*
|
||||
# match2_loc "The sun's shining, Hello World!" "raining" ;;
|
||||
- : bool * int = (false, -1)
|
||||
# match2_loc "The sun's shining, Hello World!" "shining" ;;
|
||||
- : bool * int = (true, 10)
|
||||
*)
|
||||
|
||||
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
|
||||
|
||||
(*
|
||||
# match2_num "This cloud looks like a camel, \
|
||||
that other cloud looks like a llama" "stone" ;;
|
||||
- : bool * int = (false, 0)
|
||||
# match2_num "This cloud looks like a camel, \
|
||||
that other cloud looks like a llama" "cloud" ;;
|
||||
- : bool * int = (true, 2)
|
||||
*)
|
||||
32
Task/String-matching/PL-I/string-matching.pli
Normal file
32
Task/String-matching/PL-I/string-matching.pli
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* 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);
|
||||
|
||||
/* Optional extra: 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;
|
||||
21
Task/String-matching/Perl/string-matching.pl
Normal file
21
Task/String-matching/Perl/string-matching.pl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Using regexes:
|
||||
|
||||
$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
|
||||
|
||||
# Using index:
|
||||
|
||||
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
|
||||
|
||||
# Using substr:
|
||||
|
||||
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
|
||||
|
||||
# Bonus task (printing all positions where $str2 appears in $str1):
|
||||
|
||||
print $-[0], "\n" while $str1 =~ /\Q$str2\E/g; # using a regex
|
||||
my $i = -1; print $i, "\n" while ($i = index $str1, $str2, $i + 1) != -1; # using index
|
||||
18
Task/String-matching/Pluto/string-matching.pluto
Normal file
18
Task/String-matching/Pluto/string-matching.pluto
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
local s = "abracadabra"
|
||||
local t = "abra"
|
||||
local u = "ra"
|
||||
local v = "cad"
|
||||
print($"'{s}' starts with '{t}' is {s:startswith(t)}")
|
||||
local indices = {}
|
||||
local start = 1
|
||||
while true do
|
||||
local ix = s:find(u, start, true)
|
||||
if ix then
|
||||
indices:insert(ix)
|
||||
start = ix + #u
|
||||
if start > #s then break end
|
||||
else break end
|
||||
end
|
||||
local contained = #indices > 0
|
||||
print($"'{s}' contains '{u}' is {contained} {contained ? $"at indices {indices:concat(", ")}" : ""}")
|
||||
print($"'{s}' ends with '{v}' is {s:endswith(v)}")
|
||||
5
Task/String-matching/PowerShell/string-matching.ps1
Normal file
5
Task/String-matching/PowerShell/string-matching.ps1
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"spicywiener".StartsWith("spicy")
|
||||
"spicywiener".Contains("icy")
|
||||
"spicywiener".EndsWith("wiener")
|
||||
"spicywiener".IndexOf("icy")
|
||||
[regex]::Matches("spicywiener", "i").count
|
||||
74
Task/String-matching/PureBasic/string-matching.basic
Normal file
74
Task/String-matching/PureBasic/string-matching.basic
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
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
|
||||
|
||||
; And a verification
|
||||
|
||||
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
|
||||
|
||||
; An alternate and more complete solution:
|
||||
|
||||
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
|
||||
271
Task/String-matching/RISC-V-Assembly/string-matching.asm
Normal file
271
Task/String-matching/RISC-V-Assembly/string-matching.asm
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
# riscv assembly raspberry pico2 rp2350
|
||||
# program matchstring.s
|
||||
# connexion putty com3
|
||||
/*********************************************/
|
||||
/* CONSTANTES */
|
||||
/********************************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../constantesRiscv.inc"
|
||||
/*******************************************/
|
||||
/* INITIALED DATAS */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessStart: .asciz "Program riscv start.\r\n"
|
||||
szMessEndOk: .asciz "Program riscv end OK.\r\n"
|
||||
szCariageReturn: .asciz "\r\n"
|
||||
|
||||
szMessErrorString: .asciz "Error search string empty "
|
||||
szMessFound: .asciz "String found. \n"
|
||||
szMessFoundPos: .asciz "String found at position "
|
||||
szMessNotFound: .asciz "String not found. \n"
|
||||
szString: .asciz "abcdefghijklmnopqrstuvwxyz"
|
||||
szString2: .asciz "abc"
|
||||
szStringStart: .asciz "abcd"
|
||||
szStringEnd: .asciz "xyz"
|
||||
szStringStart2: .asciz "abcd"
|
||||
szStringEnd2: .asciz "xabc"
|
||||
szString3: .asciz "abcdefddddehdezdedede"
|
||||
szStringSer: .asciz "deh"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
.align 2
|
||||
/*******************************************/
|
||||
/* UNINITIALED DATA */
|
||||
/*******************************************/
|
||||
.bss
|
||||
sConvArea: .skip 24
|
||||
.align 2
|
||||
|
||||
/**********************************************/
|
||||
/* SECTION CODE */
|
||||
/**********************************************/
|
||||
.text
|
||||
.global main
|
||||
|
||||
main:
|
||||
call stdio_init_all # général init
|
||||
1: # start loop connexion
|
||||
li a0,0 # raz argument register
|
||||
call tud_cdc_n_connected # waiting for USB connection
|
||||
beqz a0,1b # return code = zero ?
|
||||
|
||||
la a0,szMessStart # message address
|
||||
call writeString # display message
|
||||
|
||||
la a0,szString # address input string
|
||||
la a1,szStringStart # address search string
|
||||
call searchStringDeb # Determining if the first string starts with second string
|
||||
blez a0,1f
|
||||
|
||||
la a0,szMessFound # display message
|
||||
call writeString
|
||||
j 2f
|
||||
1:
|
||||
la a0,szMessNotFound
|
||||
call writeString
|
||||
2:
|
||||
|
||||
la a0,szString # address input string
|
||||
la a1,szStringEnd # address search string
|
||||
call searchStringEnd # Determining if the first string end with second string
|
||||
blez a0,3f
|
||||
|
||||
la a0,szMessFound # display message
|
||||
call writeString
|
||||
j 4f
|
||||
3:
|
||||
la a0,szMessNotFound
|
||||
call writeString
|
||||
4:
|
||||
|
||||
la a0,szString # address input string
|
||||
la a1,szStringEnd # address search string
|
||||
call searchSubString # search string
|
||||
bltz a0,5f # < 0
|
||||
|
||||
la a1,sConvArea
|
||||
call conversion10
|
||||
la a0,szMessFoundPos # display message
|
||||
call writeString
|
||||
la a0,sConvArea # display position
|
||||
call writeString
|
||||
la a0,szCarriageReturn
|
||||
call writeString
|
||||
|
||||
j 6f
|
||||
5:
|
||||
la a0,szMessNotFound
|
||||
call writeString
|
||||
6:
|
||||
|
||||
la a0,szString3 # address input string
|
||||
la a1,szStringSer # address search string
|
||||
call searchSubString # search string
|
||||
bltz a0,7f # < 0
|
||||
|
||||
la a1,sConvArea
|
||||
call conversion10
|
||||
la a0,szMessFoundPos # display message
|
||||
call writeString
|
||||
la a0,sConvArea # display position
|
||||
call writeString
|
||||
la a0,szCarriageReturn
|
||||
call writeString
|
||||
|
||||
j 8f
|
||||
7:
|
||||
la a0,szMessNotFound
|
||||
call writeString
|
||||
8:
|
||||
|
||||
la a0,szMessEndOk # message address
|
||||
call writeString # display message
|
||||
call getchar
|
||||
100: # final loop
|
||||
j 100b
|
||||
/***************************************************/
|
||||
/* search string at beguining */
|
||||
/***************************************************/
|
||||
# a0 contains string
|
||||
# a1 contains search string
|
||||
# a0 returns 1 if find or 0 if not or -1 if error
|
||||
searchStringDeb:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp) # save registers @ save registers
|
||||
li t1,0 # init counter
|
||||
add t0,a1,t1 # compute byte address
|
||||
lbu t2,(t0) # load byte string
|
||||
beqz t2,99f # empty string -> error
|
||||
1: # loop to copy string begin
|
||||
add t0,a0,t1 # compute byte address string 1
|
||||
lbu t3,(t0) # load byte string
|
||||
beqz t3,2f # zero final ? -> not found
|
||||
bne t3,t2,2f # not equal
|
||||
addi t1,t1,1
|
||||
add t0,a1,t1 # compute byte address search string
|
||||
lbu t2,(t0) # load byte string
|
||||
bnez t2,1b # not end search string -> loop
|
||||
li a0,1 # else string founded
|
||||
j 100f
|
||||
2:
|
||||
li a0,0
|
||||
j 100f
|
||||
|
||||
99: # error
|
||||
la a0,szMessErrorString
|
||||
call writeString # display message
|
||||
li a0,-1 # error
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
/***************************************************/
|
||||
/* search string at end first string */
|
||||
/***************************************************/
|
||||
# a0 contains string
|
||||
# a1 contains search string
|
||||
# a0 returns 1 if find or 0 if not or -1 if error
|
||||
searchStringEnd:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp) # save registers @ save registers
|
||||
li t1,0 # init counter
|
||||
1: # loop to search end search string
|
||||
add t0,a1,t1 # compute byte address
|
||||
lbu t2,(t0) # load byte string
|
||||
beqz t2,2f # zero final
|
||||
addi t1,t1,1 # increment indice
|
||||
j 1b # and loop
|
||||
2:
|
||||
beqz t1,99f # empty string -> error
|
||||
li t3,0
|
||||
3: # loop to search end string
|
||||
add t0,a0,t3 # compute byte address string 1
|
||||
lbu t4,(t0) # load byte string
|
||||
beqz t4,4f # zero final ?
|
||||
addi t3,t3,1
|
||||
j 3b
|
||||
4:
|
||||
beqz t3,7f # empty string -> not found
|
||||
5:
|
||||
addi t1,t1,-1
|
||||
bltz t1,6f # begin search string -> found
|
||||
add t0,a1,t1 # compute byte address
|
||||
lbu t2,(t0) # load byte string
|
||||
addi t3,t3,-1
|
||||
bltz t3,7f # begin string -> not found
|
||||
add t0,a0,t3 # compute byte address string 1
|
||||
lbu t4,(t0) # load byte string
|
||||
bne t4,t2,7f # not equal
|
||||
j 5b
|
||||
6:
|
||||
li a0,1 # string founded
|
||||
j 100f
|
||||
7: # string not found
|
||||
li a0,0
|
||||
j 100f
|
||||
|
||||
99: # error
|
||||
la a0,szMessErrorString
|
||||
call writeString # display message
|
||||
li a0,-1 # error
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
/***************************************************/
|
||||
/* search string in string */
|
||||
/***************************************************/
|
||||
# a0 contains string
|
||||
# a1 contains search string
|
||||
# a0 returns 1 if find or 0 if not or -1 if error
|
||||
searchSubString:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp) # save registers @ save registers
|
||||
li t3,0
|
||||
1:
|
||||
li t5,-1 # indice find ok
|
||||
li t1,0 # init counter
|
||||
add t0,a1,t1 # compute byte address
|
||||
lbu t2,(t0) # load byte string
|
||||
beqz t2,99f # empty string -> error
|
||||
2: # loop to copy string begin
|
||||
add t0,a0,t3 # compute byte address string 1
|
||||
lbu t4,(t0) # load byte string
|
||||
beqz t4,5f # zero final ?
|
||||
bne t4,t2,3f # not equal
|
||||
addi t5,t5,1 # increment indice find
|
||||
addi t3,t3,1 # increment indice string
|
||||
addi t1,t1,1 # increment indice search string
|
||||
add t0,a1,t1 # compute byte address
|
||||
lbu t2,(t0) # load byte string
|
||||
beqz t2,6f # end search string -> found
|
||||
j 2b # else loop
|
||||
3:
|
||||
bltz t5,4f # not characters precedent found ?
|
||||
sub t3,t3,t5 # yes raz position search
|
||||
#addi t3,t3,1
|
||||
j 1b # and restart search at begining
|
||||
4:
|
||||
addi t3,t3,1 # increment indice
|
||||
j 2b # and loop other character
|
||||
5:
|
||||
li a0,-1 # not found
|
||||
j 100f
|
||||
|
||||
6:
|
||||
sub a0,t3,t5 # string found
|
||||
addi a0,a0,-1
|
||||
j 100f
|
||||
99: # error
|
||||
la a0,szMessErrorString
|
||||
call writeString # display message
|
||||
li a0,-2 # error
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
/************************************/
|
||||
/* file include Fonctions */
|
||||
/***********************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../includeFunctions.s"
|
||||
21
Task/String-matching/Raku/string-matching.raku
Normal file
21
Task/String-matching/Raku/string-matching.raku
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Using string methods:
|
||||
|
||||
$haystack.starts-with($needle) # True if $haystack starts with $needle
|
||||
$haystack.contains($needle) # True if $haystack contains $needle
|
||||
$haystack.ends-with($needle) # True if $haystack ends with $needle
|
||||
|
||||
# Using regexes:
|
||||
|
||||
so $haystack ~~ /^ $needle / # True if $haystack starts with $needle
|
||||
so $haystack ~~ / $needle / # True if $haystack contains $needle
|
||||
so $haystack ~~ / $needle $/ # True if $haystack ends with $needle
|
||||
|
||||
# Using substr:
|
||||
|
||||
substr($haystack, 0, $needle.chars) eq $needle # True if $haystack starts with $needle
|
||||
substr($haystack, *-$needle.chars) eq $needle # True if $haystack ends with $needle
|
||||
|
||||
# Bonus task:
|
||||
|
||||
$haystack.match($needle, :g)».from; # List of all positions where $needle appears in $haystack
|
||||
$haystack.indices($needle :overlap); # Also find any overlapping instances of $needle in $haystack
|
||||
11
Task/String-matching/Sed/string-matching.sed
Normal file
11
Task/String-matching/Sed/string-matching.sed
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# 1. Determining if the first string starts with the second string:
|
||||
|
||||
N;/^\(.*\).*\n\1$/!d;s/\n.*//
|
||||
|
||||
# 2. Determining if the first string contains the second string at any location:
|
||||
|
||||
N;/.*\(.*\).*\n\1$/!d;s/\n.*//
|
||||
|
||||
# 3. Determining if the first string ends with the second string:
|
||||
|
||||
N;/\(.*\)\n\1$/!d;s/\n.*//
|
||||
43
Task/String-matching/VBScript/string-matching.vbs
Normal file
43
Task/String-matching/VBScript/string-matching.vbs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
Function StartsWith(s1,s2)
|
||||
StartsWith = False
|
||||
If Left(s1,Len(s2)) = s2 Then
|
||||
StartsWith = True
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function Contains(s1,s2)
|
||||
Contains = False
|
||||
If InStr(1,s1,s2) Then
|
||||
Contains = True & " at positions "
|
||||
j = 1
|
||||
Do Until InStr(j,s1,s2) = False
|
||||
Contains = Contains & InStr(j,s1,s2) & ", "
|
||||
If j = 1 Then
|
||||
If Len(s2) = 1 Then
|
||||
j = j + InStr(j,s1,s2)
|
||||
Else
|
||||
j = j + (InStr(j,s1,s2) + (Len(s2) - 1))
|
||||
End If
|
||||
Else
|
||||
If Len(s2) = 1 Then
|
||||
j = j + ((InStr(j,s1,s2) - j) + 1)
|
||||
Else
|
||||
j = j + ((InStr(j,s1,s2) - j) + (Len(s2) - 1))
|
||||
End If
|
||||
End If
|
||||
Loop
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function EndsWith(s1,s2)
|
||||
EndsWith = False
|
||||
If Right(s1,Len(s2)) = s2 Then
|
||||
EndsWith = True
|
||||
End If
|
||||
End Function
|
||||
|
||||
WScript.StdOut.Write "Starts with test, 'foo' in 'foobar': " & StartsWith("foobar","foo")
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "Contains test, 'o' in 'fooooobar': " & Contains("fooooobar","o")
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "Ends with test, 'bar' in 'foobar': " & EndsWith("foobar","bar")
|
||||
Loading…
Add table
Add a link
Reference in a new issue