Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
3
Task/String-matching/00-META.yaml
Normal file
3
Task/String-matching/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/String_matching
|
||||
note: String manipulation
|
||||
20
Task/String-matching/00-TASK.txt
Normal file
20
Task/String-matching/00-TASK.txt
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{{basic data operation}}
|
||||
[[Category: String manipulation]]
|
||||
[[Category:Simple]]
|
||||
|
||||
;Task:
|
||||
Given two strings, demonstrate the following three types of string matching:
|
||||
|
||||
::# 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
|
||||
|
||||
<br>
|
||||
Optional requirements:
|
||||
::# Print the location of the match for part 2
|
||||
::# Handle multiple occurrences of a string for part 2.
|
||||
|
||||
|
||||
{{Template:Strings}}
|
||||
<br><br>
|
||||
|
||||
6
Task/String-matching/11l/string-matching.11l
Normal file
6
Task/String-matching/11l/string-matching.11l
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
print(‘abcd’.starts_with(‘ab’))
|
||||
print(‘abcd’.ends_with(‘zn’))
|
||||
print(‘bb’ C ‘abab’)
|
||||
print(‘ab’ C ‘abab’)
|
||||
print(‘abab’.find(‘bb’) ? -1)
|
||||
print(‘abab’.find(‘ab’) ? -1)
|
||||
34
Task/String-matching/360-Assembly/string-matching.360
Normal file
34
Task/String-matching/360-Assembly/string-matching.360
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
* String matching 04/04/2017
|
||||
STRMATCH CSECT
|
||||
USING STRMATCH,R15
|
||||
XPRNT SS,L'SS
|
||||
*
|
||||
CLC SS(L'S1),S1
|
||||
BNE NOT1
|
||||
XPRNT =C'-- STARTS WITH',14
|
||||
XPRNT S1,L'S1
|
||||
NOT1 EQU *
|
||||
*
|
||||
CLC SS+L'SS-L'S2(L'S2),S2
|
||||
BNE NOT2
|
||||
XPRNT =C'-- ENDS WITH',12
|
||||
XPRNT S2,L'S2
|
||||
NOT2 EQU *
|
||||
*
|
||||
LA R0,L'SS-L'S3+1
|
||||
LA R1,SS
|
||||
LOOP CLC 0(L'S3,R1),S3
|
||||
BNE NOT3
|
||||
XPRNT =C'-- CONTAINS',11
|
||||
XPRNT S3,L'S3
|
||||
NOT3 LA R1,1(R1)
|
||||
BCT R0,LOOP
|
||||
*
|
||||
BR R14
|
||||
SS DC CL6'ABCDEF'
|
||||
S1 DC CL2'AB'
|
||||
S2 DC CL2'EF'
|
||||
S3 DC CL2'CD'
|
||||
PG DC CL80' '
|
||||
YREGS
|
||||
END STRMATCH
|
||||
250
Task/String-matching/AArch64-Assembly/string-matching.aarch64
Normal file
250
Task/String-matching/AArch64-Assembly/string-matching.aarch64
Normal file
|
|
@ -0,0 +1,250 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program strMatching64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessFound: .asciz "String found. \n"
|
||||
szMessNotFound: .asciz "String not found. \n"
|
||||
szString: .asciz "abcdefghijklmnopqrstuvwxyz"
|
||||
szString2: .asciz "abc"
|
||||
szStringStart: .asciz "abcd"
|
||||
szStringEnd: .asciz "xyz"
|
||||
szStringStart2: .asciz "abcd"
|
||||
szStringEnd2: .asciz "xabc"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
|
||||
ldr x0,qAdrszString // address input string
|
||||
ldr x1,qAdrszStringStart // address search string
|
||||
|
||||
bl searchStringDeb // Determining if the first string starts with second string
|
||||
cmp x0,0
|
||||
ble 1f
|
||||
ldr x0,qAdrszMessFound // display message
|
||||
bl affichageMess
|
||||
b 2f
|
||||
1:
|
||||
ldr x0,qAdrszMessNotFound
|
||||
bl affichageMess
|
||||
2:
|
||||
ldr x0,qAdrszString // address input string
|
||||
ldr x1,qAdrszStringEnd // address search string
|
||||
bl searchStringFin // Determining if the first string ends with the second string
|
||||
cmp x0,0
|
||||
ble 3f
|
||||
ldr x0,qAdrszMessFound // display message
|
||||
bl affichageMess
|
||||
b 4f
|
||||
3:
|
||||
ldr x0,qAdrszMessNotFound
|
||||
bl affichageMess
|
||||
4:
|
||||
ldr x0,qAdrszString2 // address input string
|
||||
ldr x1,qAdrszStringStart2 // address search string
|
||||
|
||||
bl searchStringDeb //
|
||||
cmp x0,0
|
||||
ble 5f
|
||||
ldr x0,qAdrszMessFound // display message
|
||||
bl affichageMess
|
||||
b 6f
|
||||
5:
|
||||
ldr x0,qAdrszMessNotFound
|
||||
bl affichageMess
|
||||
6:
|
||||
ldr x0,qAdrszString2 // address input string
|
||||
ldr x1,qAdrszStringEnd2 // address search string
|
||||
bl searchStringFin
|
||||
cmp x0,0
|
||||
ble 7f
|
||||
ldr x0,qAdrszMessFound // display message
|
||||
bl affichageMess
|
||||
b 8f
|
||||
7:
|
||||
ldr x0,qAdrszMessNotFound
|
||||
bl affichageMess
|
||||
8:
|
||||
ldr x0,qAdrszString // address input string
|
||||
ldr x1,qAdrszStringEnd // address search string
|
||||
bl searchSubString // Determining if the first string contains the second string at any location
|
||||
cmp x0,0
|
||||
ble 9f
|
||||
ldr x0,qAdrszMessFound // display message
|
||||
bl affichageMess
|
||||
b 10f
|
||||
9:
|
||||
ldr x0,qAdrszMessNotFound // display substring result
|
||||
bl affichageMess
|
||||
10:
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform system call
|
||||
qAdrszMessFound: .quad szMessFound
|
||||
qAdrszMessNotFound: .quad szMessNotFound
|
||||
qAdrszString: .quad szString
|
||||
qAdrszString2: .quad szString2
|
||||
qAdrszStringStart: .quad szStringStart
|
||||
qAdrszStringEnd: .quad szStringEnd
|
||||
qAdrszStringStart2: .quad szStringStart2
|
||||
qAdrszStringEnd2: .quad szStringEnd2
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
/******************************************************************/
|
||||
/* search substring at begin of input string */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of the input string */
|
||||
/* x1 contains the address of substring */
|
||||
/* x0 returns 1 if find or 0 if not or -1 if error */
|
||||
searchStringDeb:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
mov x3,0 // counter byte string
|
||||
ldrb w4,[x1,x3] // load first byte of substring
|
||||
cbz x4,99f // empty string ?
|
||||
1:
|
||||
ldrb w2,[x0,x3] // load byte string input
|
||||
cbz x2,98f // zero final ?
|
||||
cmp x4,x2 // bytes equals ?
|
||||
bne 98f // no not find
|
||||
add x3,x3,1 // increment counter
|
||||
ldrb w4,[x1,x3] // and load next byte of substring
|
||||
cbnz x4,1b // zero final ?
|
||||
mov x0,1 // yes is ok
|
||||
b 100f
|
||||
98:
|
||||
mov x0,0 // not find
|
||||
b 100f
|
||||
99:
|
||||
mov x0,-1 // error
|
||||
100:
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
/******************************************************************/
|
||||
/* search substring at end of input string */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of the input string */
|
||||
/* x1 contains the address of substring */
|
||||
/* x0 returns 1 if find or 0 if not or -1 if error */
|
||||
searchStringFin:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
stp x4,x5,[sp,-16]! // save registers
|
||||
mov x3,0 // counter byte string
|
||||
// search the last character of substring
|
||||
1:
|
||||
ldrb w4,[x1,x3] // load byte of substring
|
||||
cmp x4,#0 // zero final ?
|
||||
add x2,x3,1
|
||||
csel x3,x2,x3,ne // no increment counter
|
||||
//addne x3,#1 // no increment counter
|
||||
bne 1b // and loop
|
||||
cbz x3,99f // empty string ?
|
||||
|
||||
sub x3,x3,1 // index of last byte
|
||||
ldrb w4,[x1,x3] // load last byte of substring
|
||||
// search the last character of string
|
||||
mov x2,0 // index last character
|
||||
2:
|
||||
ldrb w5,[x0,x2] // load first byte of substring
|
||||
cmp x5,0 // zero final ?
|
||||
add x5,x2,1 // no -> increment counter
|
||||
csel x2,x5,x2,ne
|
||||
//addne x2,#1 // no -> increment counter
|
||||
bne 2b // and loop
|
||||
cbz x2,98f // empty input string ?
|
||||
sub x2,x2,1 // index last character
|
||||
3:
|
||||
ldrb w5,[x0,x2] // load byte string input
|
||||
cmp x4,x5 // bytes equals ?
|
||||
bne 98f // no -> not found
|
||||
subs x3,x3,1 // decrement counter
|
||||
blt 97f // ok found
|
||||
subs x2,x2,1 // decrement counter input string
|
||||
blt 98f // if zero -> not found
|
||||
ldrb w4,[x1,x3] // load previous byte of substring
|
||||
b 3b // and loop
|
||||
97:
|
||||
mov x0,1 // yes is ok
|
||||
b 100f
|
||||
98:
|
||||
mov x0,0 // not found
|
||||
b 100f
|
||||
99:
|
||||
mov x0,-1 // error
|
||||
100:
|
||||
ldp x4,x5,[sp],16 // restaur 2 registers
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
/******************************************************************/
|
||||
/* search a substring in the string */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of the input string */
|
||||
/* x1 contains the address of substring */
|
||||
/* x0 returns index of substring in string or -1 if not found */
|
||||
searchSubString:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
stp x4,x5,[sp,-16]! // save registers
|
||||
mov x2,0 // counter byte input string
|
||||
mov x3,0 // counter byte string
|
||||
mov x6,-1 // index found
|
||||
ldrb w4,[x1,x3]
|
||||
1:
|
||||
ldrb w5,[x0,x2] // load byte string
|
||||
cbz x5,99f // zero final ?
|
||||
cmp x5,x4 // compare character
|
||||
beq 2f
|
||||
mov x6,-1 // no equals - > raz index
|
||||
mov x3,0 // and raz counter byte
|
||||
add x2,x2,1 // and increment counter byte
|
||||
b 1b // and loop
|
||||
2: // characters equals
|
||||
cmp x6,-1 // first characters equals ?
|
||||
csel x6,x2,x6,eq // yes -> index begin in x6
|
||||
//moveq x6,x2 // yes -> index begin in x6
|
||||
add x3,x3,1 // increment counter substring
|
||||
ldrb w4,[x1,x3] // and load next byte
|
||||
cmp x4,0 // zero final ?
|
||||
beq 3f // yes -> end search
|
||||
add x2,x2,1 // else increment counter string
|
||||
b 1b // and loop
|
||||
3:
|
||||
mov x0,x6
|
||||
b 100f
|
||||
|
||||
98:
|
||||
mov x0,0 // not found
|
||||
b 100f
|
||||
99:
|
||||
mov x0,-1 // error
|
||||
100:
|
||||
ldp x4,x5,[sp],16 // restaur 2 registers
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
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 #
|
||||
))
|
||||
240
Task/String-matching/ARM-Assembly/string-matching.arm
Normal file
240
Task/String-matching/ARM-Assembly/string-matching.arm
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program strMatching.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessFound: .asciz "String found. \n"
|
||||
szMessNotFound: .asciz "String not found. \n"
|
||||
szString: .asciz "abcdefghijklmnopqrstuvwxyz"
|
||||
szString2: .asciz "abc"
|
||||
szStringStart: .asciz "abcd"
|
||||
szStringEnd: .asciz "xyz"
|
||||
szStringStart2: .asciz "abcd"
|
||||
szStringEnd2: .asciz "xabc"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
|
||||
ldr r0,iAdrszString @ address input string
|
||||
ldr r1,iAdrszStringStart @ address search string
|
||||
|
||||
bl searchStringDeb @ Determining if the first string starts with second string
|
||||
cmp r0,#0
|
||||
ble 1f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 2f
|
||||
1:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
2:
|
||||
ldr r0,iAdrszString @ address input string
|
||||
ldr r1,iAdrszStringEnd @ address search string
|
||||
bl searchStringFin @ Determining if the first string ends with the second string
|
||||
cmp r0,#0
|
||||
ble 3f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 4f
|
||||
3:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
4:
|
||||
ldr r0,iAdrszString2 @ address input string
|
||||
ldr r1,iAdrszStringStart2 @ address search string
|
||||
|
||||
bl searchStringDeb @
|
||||
cmp r0,#0
|
||||
ble 5f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 6f
|
||||
5:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
6:
|
||||
ldr r0,iAdrszString2 @ address input string
|
||||
ldr r1,iAdrszStringEnd2 @ address search string
|
||||
bl searchStringFin
|
||||
cmp r0,#0
|
||||
ble 7f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 8f
|
||||
7:
|
||||
ldr r0,iAdrszMessNotFound
|
||||
bl affichageMess
|
||||
8:
|
||||
ldr r0,iAdrszString @ address input string
|
||||
ldr r1,iAdrszStringEnd @ address search string
|
||||
bl searchSubString @ Determining if the first string contains the second string at any location
|
||||
cmp r0,#0
|
||||
ble 9f
|
||||
ldr r0,iAdrszMessFound @ display message
|
||||
bl affichageMess
|
||||
b 10f
|
||||
9:
|
||||
ldr r0,iAdrszMessNotFound @ display substring result
|
||||
bl affichageMess
|
||||
10:
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc 0 @ perform system call
|
||||
iAdrszMessFound: .int szMessFound
|
||||
iAdrszMessNotFound: .int szMessNotFound
|
||||
iAdrszString: .int szString
|
||||
iAdrszString2: .int szString2
|
||||
iAdrszStringStart: .int szStringStart
|
||||
iAdrszStringEnd: .int szStringEnd
|
||||
iAdrszStringStart2: .int szStringStart2
|
||||
iAdrszStringEnd2: .int szStringEnd2
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
/******************************************************************/
|
||||
/* search substring at begin of input string */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the input string */
|
||||
/* r1 contains the address of substring */
|
||||
/* r0 returns 1 if find or 0 if not or -1 if error */
|
||||
searchStringDeb:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string
|
||||
ldrb r4,[r1,r3] @ load first byte of substring
|
||||
cmp r4,#0 @ empty string ?
|
||||
moveq r0,#-1 @ error
|
||||
beq 100f
|
||||
1:
|
||||
ldrb r2,[r0,r3] @ load byte string input
|
||||
cmp r2,#0 @ zero final ?
|
||||
moveq r0,#0 @ not find
|
||||
beq 100f
|
||||
cmp r4,r2 @ bytes equals ?
|
||||
movne r0,#0 @ no not find
|
||||
bne 100f
|
||||
add r3,#1 @ increment counter
|
||||
ldrb r4,[r1,r3] @ and load next byte of substring
|
||||
cmp r4,#0 @ zero final ?
|
||||
bne 1b @ no -> loop
|
||||
mov r0,#1 @ yes is ok
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* search substring at end of input string */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the input string */
|
||||
/* r1 contains the address of substring */
|
||||
/* r0 returns 1 if find or 0 if not or -1 if error */
|
||||
searchStringFin:
|
||||
push {r1-r5,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string
|
||||
@ search the last character of substring
|
||||
1:
|
||||
ldrb r4,[r1,r3] @ load byte of substring
|
||||
cmp r4,#0 @ zero final ?
|
||||
addne r3,#1 @ no increment counter
|
||||
bne 1b @ and loop
|
||||
cmp r3,#0 @ empty string ?
|
||||
moveq r0,#-1 @ error
|
||||
beq 100f
|
||||
sub r3,#1 @ index of last byte
|
||||
ldrb r4,[r1,r3] @ load last byte of substring
|
||||
@ search the last character of string
|
||||
mov r2,#0 @ index last character
|
||||
2:
|
||||
ldrb r5,[r0,r2] @ load first byte of substring
|
||||
cmp r5,#0 @ zero final ?
|
||||
addne r2,#1 @ no -> increment counter
|
||||
bne 2b @ and loop
|
||||
cmp r2,#0 @ empty input string ?
|
||||
moveq r0,#0 @ yes -> not found
|
||||
beq 100f
|
||||
sub r2,#1 @ index last character
|
||||
3:
|
||||
ldrb r5,[r0,r2] @ load byte string input
|
||||
cmp r4,r5 @ bytes equals ?
|
||||
movne r0,#0 @ no -> not found
|
||||
bne 100f
|
||||
subs r3,#1 @ decrement counter
|
||||
movlt r0,#1 @ if zero -> ok found
|
||||
blt 100f
|
||||
subs r2,#1 @ decrement counter input string
|
||||
movlt r0,#0 @ if zero -> not found
|
||||
blt 100f
|
||||
ldrb r4,[r1,r3] @ load previous byte of substring
|
||||
b 3b @ and loop
|
||||
|
||||
100:
|
||||
pop {r1-r5,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* search a substring in the string */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the input string */
|
||||
/* r1 contains the address of substring */
|
||||
/* r0 returns index of substring in string or -1 if not found */
|
||||
searchSubString:
|
||||
push {r1-r6,lr} @ save registers
|
||||
mov r2,#0 @ counter byte input string
|
||||
mov r3,#0 @ counter byte string
|
||||
mov r6,#-1 @ index found
|
||||
ldrb r4,[r1,r3]
|
||||
1:
|
||||
ldrb r5,[r0,r2] @ load byte string
|
||||
cmp r5,#0 @ zero final ?
|
||||
moveq r0,#-1 @ yes returns error
|
||||
beq 100f
|
||||
cmp r5,r4 @ compare character
|
||||
beq 2f
|
||||
mov r6,#-1 @ no equals - > raz index
|
||||
mov r3,#0 @ and raz counter byte
|
||||
add r2,#1 @ and increment counter byte
|
||||
b 1b @ and loop
|
||||
2: @ characters equals
|
||||
cmp r6,#-1 @ first characters equals ?
|
||||
moveq r6,r2 @ yes -> index begin in r6
|
||||
add r3,#1 @ increment counter substring
|
||||
ldrb r4,[r1,r3] @ and load next byte
|
||||
cmp r4,#0 @ zero final ?
|
||||
beq 3f @ yes -> end search
|
||||
add r2,#1 @ else increment counter string
|
||||
b 1b @ and loop
|
||||
3:
|
||||
mov r0,r6
|
||||
100:
|
||||
pop {r1-r6,lr} @ restaur registers
|
||||
bx lr
|
||||
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registers
|
||||
mov r2,#0 @ counter length */
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call system
|
||||
pop {r0,r1,r2,r7,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
17
Task/String-matching/AWK/string-matching.awk
Normal file
17
Task/String-matching/AWK/string-matching.awk
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/awk -f
|
||||
{ pos=index($2,$1)
|
||||
print $2, (pos==1 ? "begins" : "does not begin" ), "with " $1
|
||||
print $2, (pos ? "contains an" : "does not contain" ), "\"" $1 "\""
|
||||
if (pos) {
|
||||
l=length($1)
|
||||
Pos=pos
|
||||
s=$2
|
||||
while (Pos){
|
||||
print " " $1 " is at index", x+Pos
|
||||
x+=Pos
|
||||
s=substr(s,Pos+l)
|
||||
Pos=index(s,$1)
|
||||
}
|
||||
}
|
||||
print $2, (substr($2,pos)==$1 ? "ends" : "does not end"), "with " $1
|
||||
}
|
||||
110
Task/String-matching/Action-/string-matching.action
Normal file
110
Task/String-matching/Action-/string-matching.action
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
BYTE FUNC FindS(CHAR ARRAY text,sub BYTE start)
|
||||
BYTE i,j,found
|
||||
|
||||
i=start
|
||||
WHILE i<=text(0)-sub(0)+1
|
||||
DO
|
||||
found=0
|
||||
FOR j=1 TO sub(0)
|
||||
DO
|
||||
IF text(i+j-1)#sub(j) THEN
|
||||
found=0 EXIT
|
||||
ELSE
|
||||
found=1
|
||||
FI
|
||||
OD
|
||||
IF found THEN
|
||||
RETURN (i)
|
||||
FI
|
||||
i==+1
|
||||
OD
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC StartsWith(CHAR ARRAY text,sub)
|
||||
BYTE pos
|
||||
|
||||
pos=FindS(text,sub,1)
|
||||
IF pos=1 THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC EndsWith(CHAR ARRAY text,sub)
|
||||
BYTE pos,start
|
||||
|
||||
IF sub(0)>text(0) THEN
|
||||
RETURN (0)
|
||||
FI
|
||||
start=text(0)-sub(0)+1
|
||||
pos=FindS(text,sub,start)
|
||||
IF pos=start THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC Contains(CHAR ARRAY text,sub
|
||||
BYTE ARRAY positions)
|
||||
BYTE pos,count
|
||||
|
||||
pos=1 count=0
|
||||
WHILE pos<=text(0)
|
||||
DO
|
||||
pos=FindS(text,sub,pos)
|
||||
IF pos>0 THEN
|
||||
positions(count)=pos
|
||||
count==+1
|
||||
pos==+1
|
||||
ELSE
|
||||
EXIT
|
||||
FI
|
||||
OD
|
||||
RETURN (count)
|
||||
|
||||
PROC TestStartsWith(CHAR ARRAY text,sub)
|
||||
IF StartsWith(text,sub) THEN
|
||||
PrintF("""%S"" starts with ""%S"".%E",text,sub)
|
||||
ELSE
|
||||
PrintF("""%S"" does not start with ""%S"".%E",text,sub)
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC TestEndsWith(CHAR ARRAY text,sub)
|
||||
IF EndsWith(text,sub) THEN
|
||||
PrintF("""%S"" ends with ""%S"".%E",text,sub)
|
||||
ELSE
|
||||
PrintF("""%S"" does not end with ""%S"".%E",text,sub)
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC TestContains(CHAR ARRAY text,sub)
|
||||
BYTE ARRAY positions(20)
|
||||
BYTE i,count
|
||||
|
||||
count=Contains(text,sub,positions)
|
||||
IF count>0 THEN
|
||||
PrintF("""%S"" contains %B ""%S"" at positions:",text,count,sub)
|
||||
FOR i=0 TO count-1
|
||||
DO
|
||||
PrintB(positions(i))
|
||||
IF i<count-1 THEN
|
||||
Print(", ")
|
||||
ELSE
|
||||
PrintE(".")
|
||||
FI
|
||||
OD
|
||||
ELSE
|
||||
PrintF("""%S"" does not contain ""%S"".%E",text,sub)
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
TestStartsWith("1234abc","123")
|
||||
TestStartsWith("1234abc","234")
|
||||
PutE()
|
||||
TestContains("abbaabab","ab")
|
||||
TestContains("abbaabab","ba")
|
||||
TestContains("abbaabab","xyz")
|
||||
PutE()
|
||||
TestEndsWith("1234abc","abc")
|
||||
TestEndsWith("1234abc","ab")
|
||||
RETURN
|
||||
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;
|
||||
22
Task/String-matching/Aime/string-matching.aime
Normal file
22
Task/String-matching/Aime/string-matching.aime
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
text t;
|
||||
data b;
|
||||
|
||||
b = "Bangkok";
|
||||
|
||||
t = "Bang";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b.seek(t) == 0,
|
||||
b.seek(t) != -1,
|
||||
b.seek(t) != -1 && b.seek(t) + ~t == ~b);
|
||||
|
||||
t = "ok";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b.seek(t) == 0,
|
||||
b.seek(t) != -1,
|
||||
b.seek(t) != -1 && b.seek(t) + ~t == ~b);
|
||||
|
||||
t = "Summer";
|
||||
|
||||
o_form("starts with, embeds, ends with \"~\": ~, ~, ~\n", t, b.seek(t) == 0,
|
||||
b.seek(t) != -1,
|
||||
b.seek(t) != -1 && b.seek(t) + ~t == ~b);
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
set stringA to "I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy."
|
||||
|
||||
set string1 to "I felt happy"
|
||||
set string2 to "I should feel happy"
|
||||
set string3 to "I wasn't really happy"
|
||||
|
||||
-- Determining if the first string starts with second string
|
||||
stringA starts with string1 --> true
|
||||
|
||||
-- Determining if the first string contains the second string at any location
|
||||
stringA contains string2 --> true
|
||||
|
||||
-- Determining if the first string ends with the second string
|
||||
stringA ends with string3 --> false
|
||||
|
||||
-- Print the location of the match for part 2
|
||||
offset of string2 in stringA --> 69
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
-- Handle multiple occurrences of a string for part 2
|
||||
on offset of needle in haystack
|
||||
local needle, haystack
|
||||
|
||||
if the needle is not in the haystack then return {}
|
||||
set my text item delimiters to the needle
|
||||
script
|
||||
property N : needle's length
|
||||
property t : {1 - N} & haystack's text items
|
||||
end script
|
||||
|
||||
tell the result
|
||||
repeat with i from 2 to (its t's length) - 1
|
||||
set x to item i of its t
|
||||
set y to item (i - 1) of its t
|
||||
set item i of its t to (its N) + (x's length) + y
|
||||
end repeat
|
||||
|
||||
items 2 thru -2 of its t
|
||||
end tell
|
||||
end offset
|
||||
|
||||
offset of "happy" in stringA --> {8, 44, 83, 110}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
-- offsets :: String -> String -> [Int]
|
||||
on offsets(needle, haystack)
|
||||
script match
|
||||
property mx : length of haystack
|
||||
property d : (length of needle) - 1
|
||||
on |λ|(x, i, xs)
|
||||
set z to d + i
|
||||
mx ≥ z and needle = text i thru z of xs
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
findIndices(match, haystack)
|
||||
end offsets
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
on run
|
||||
set txt to "I felt happy because I saw the others " & ¬
|
||||
"were happy and because I knew I should " & ¬
|
||||
"feel happy, but I wasn’t really happy."
|
||||
|
||||
offsets("happy", txt)
|
||||
|
||||
--> {8, 44, 83, 110}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC -------------------------------------------------
|
||||
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
set lng to length of xs
|
||||
set acc to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set acc to acc & (|λ|(item i of xs, i, xs))
|
||||
end repeat
|
||||
end tell
|
||||
return acc
|
||||
end concatMap
|
||||
|
||||
|
||||
-- findIndices :: (a -> Bool) -> [a] -> [Int]
|
||||
-- findIndices :: (String -> Bool) -> String -> [Int]
|
||||
on findIndices(p, xs)
|
||||
script go
|
||||
property f : mReturn(p)
|
||||
on |λ|(x, i, xs)
|
||||
if f's |λ|(x, i, xs) then
|
||||
{i}
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
concatMap(go, xs)
|
||||
end findIndices
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
18
Task/String-matching/Applesoft-BASIC/string-matching.basic
Normal file
18
Task/String-matching/Applesoft-BASIC/string-matching.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$
|
||||
13
Task/String-matching/Arturo/string-matching.arturo
Normal file
13
Task/String-matching/Arturo/string-matching.arturo
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
print prefix? "abcd" "ab"
|
||||
print prefix? "abcd" "cd"
|
||||
print suffix? "abcd" "ab"
|
||||
print suffix? "abcd" "cd"
|
||||
|
||||
print contains? "abcd" "ab"
|
||||
print contains? "abcd" "xy"
|
||||
|
||||
print in? "ab" "abcd"
|
||||
print in? "xy" "abcd"
|
||||
|
||||
print index "abcd" "bc"
|
||||
print index "abcd" "xy"
|
||||
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.basic
Normal file
27
Task/String-matching/BASIC/string-matching.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
|
||||
32
Task/String-matching/BBC-BASIC/string-matching.basic
Normal file
32
Task/String-matching/BBC-BASIC/string-matching.basic
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%
|
||||
7
Task/String-matching/BQN/string-matching-1.bqn
Normal file
7
Task/String-matching/BQN/string-matching-1.bqn
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
SW ← ⊑⍷˜
|
||||
|
||||
Contains ← ∨´⍷˜
|
||||
|
||||
EW ← ¯1⊑⍷˜
|
||||
|
||||
Locs ← /⍷˜
|
||||
18
Task/String-matching/BQN/string-matching-2.bqn
Normal file
18
Task/String-matching/BQN/string-matching-2.bqn
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
"abcd" SW "ab"
|
||||
1
|
||||
"abcd" SW "cd"
|
||||
0
|
||||
"abcd" EW "ab"
|
||||
0
|
||||
"abcd" EW "cd"
|
||||
1
|
||||
"abcd" Contains "bb"
|
||||
0
|
||||
"abcd" Contains "ab"
|
||||
1
|
||||
"abcd" Contains "bc"
|
||||
1
|
||||
"abab" Contains "ab"
|
||||
1
|
||||
"abab" Locs "ab"
|
||||
⟨ 0 2 ⟩
|
||||
70
Task/String-matching/Batch-File/string-matching.bat
Normal file
70
Task/String-matching/Batch-File/string-matching.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.
|
||||
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
|
||||
13
Task/String-matching/C-sharp/string-matching.cs
Normal file
13
Task/String-matching/C-sharp/string-matching.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Program
|
||||
{
|
||||
public static void Main (string[] args)
|
||||
{
|
||||
var value = "abcd".StartsWith("ab");
|
||||
value = "abcd".EndsWith("zn"); //returns false
|
||||
value = "abab".Contains("bb"); //returns false
|
||||
value = "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
|
||||
}
|
||||
}
|
||||
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.pas
Normal file
113
Task/String-matching/Component-Pascal/string-matching.pas
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
|
||||
}
|
||||
24
Task/String-matching/DCL/string-matching.dcl
Normal file
24
Task/String-matching/DCL/string-matching.dcl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
$ first_string = p1
|
||||
$ length_of_first_string = f$length( first_string )
|
||||
$ second_string = p2
|
||||
$ length_of_second_string = f$length( second_string )
|
||||
$ offset = f$locate( second_string, first_string )
|
||||
$ if offset .eq. 0
|
||||
$ then
|
||||
$ write sys$output "first string starts with second string"
|
||||
$ else
|
||||
$ write sys$output "first string does not start with second string"
|
||||
$ endif
|
||||
$ if offset .ne. length_of_first_string
|
||||
$ then
|
||||
$ write sys$output "first string contains the second string at location ", offset
|
||||
$ else
|
||||
$ write sys$output "first string does not contain the second string at any location"
|
||||
$ endif
|
||||
$ temp = f$extract( length_of_first_string - length_of_second_string, length_of_second_string, first_string )
|
||||
$ if second_string .eqs. temp
|
||||
$ then
|
||||
$ write sys$output "first string ends with the second string"
|
||||
$ else
|
||||
$ write sys$output "first string does not end with the second string"
|
||||
$ endif
|
||||
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.
|
||||
6
Task/String-matching/Dyalect/string-matching.dyalect
Normal file
6
Task/String-matching/Dyalect/string-matching.dyalect
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
var value = "abcd".StartsWith("ab")
|
||||
value = "abcd".EndsWith("zn") //returns false
|
||||
value = "abab".Contains("bb") //returns false
|
||||
value = "abab".Contains("ab") //returns true
|
||||
var loc = "abab".IndexOf("bb") //returns -1
|
||||
loc = "abab".IndexOf("ab") //returns 0
|
||||
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))
|
||||
}
|
||||
5
Task/String-matching/EchoLisp/string-matching.l
Normal file
5
Task/String-matching/EchoLisp/string-matching.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(string-suffix? "nette" "Antoinette") → #t
|
||||
(string-prefix? "Simon" "Simon & Garfunkel") → #t
|
||||
|
||||
(string-match "Antoinette" "net") → #t ;; contains
|
||||
(string-index "net" "Antoinette") → 5 ;; substring location
|
||||
24
Task/String-matching/Elena/string-matching.elena
Normal file
24
Task/String-matching/Elena/string-matching.elena
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
var s := "abcd";
|
||||
|
||||
console.printLine(s," starts with ab: ",s.startingWith:"ab");
|
||||
console.printLine(s," starts with cd: ",s.startingWith:"cd");
|
||||
|
||||
console.printLine(s," ends with ab: ",s.endingWith:"ab");
|
||||
console.printLine(s," ends with cd: ",s.endingWith:"cd");
|
||||
|
||||
console.printLine(s," contains ab: ",s.containing:"ab");
|
||||
console.printLine(s," contains bc: ",s.containing:"bc");
|
||||
console.printLine(s," contains cd: ",s.containing:"cd");
|
||||
console.printLine(s," contains az: ",s.containing:"az");
|
||||
|
||||
console.printLine(s," index of az: ",s.indexOf(0, "az"));
|
||||
console.printLine(s," index of cd: ",s.indexOf(0, "cd"));
|
||||
console.printLine(s," index of bc: ",s.indexOf(0, "bc"));
|
||||
console.printLine(s," index of ab: ",s.indexOf(0, "ab"));
|
||||
|
||||
console.readChar()
|
||||
}
|
||||
28
Task/String-matching/Elixir/string-matching.elixir
Normal file
28
Task/String-matching/Elixir/string-matching.elixir
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
s1 = "abcd"
|
||||
s2 = "adab"
|
||||
s3 = "ab"
|
||||
|
||||
String.starts_with?(s1, s3)
|
||||
# => true
|
||||
String.starts_with?(s2, s3)
|
||||
# => false
|
||||
|
||||
String.contains?(s1, s3)
|
||||
# => true
|
||||
String.contains?(s2, s3)
|
||||
# => true
|
||||
|
||||
String.ends_with?(s1, s3)
|
||||
# => false
|
||||
String.ends_with?(s2, s3)
|
||||
# => true
|
||||
|
||||
|
||||
# Optional requirements:
|
||||
Regex.run(~r/#{s3}/, s1, return: :index)
|
||||
# => [{0, 2}]
|
||||
Regex.run(~r/#{s3}/, s2, return: :index)
|
||||
# => [{2, 2}]
|
||||
|
||||
Regex.scan(~r/#{s3}/, "abcabc", return: :index)
|
||||
# => [[{0, 2}], [{3, 2}]]
|
||||
14
Task/String-matching/Emacs-Lisp/string-matching.l
Normal file
14
Task/String-matching/Emacs-Lisp/string-matching.l
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
|
||||
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
|
||||
24
Task/String-matching/F-Sharp/string-matching.fs
Normal file
24
Task/String-matching/F-Sharp/string-matching.fs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
[<EntryPoint>]
|
||||
let main args =
|
||||
|
||||
let text = "一二三四五六七八九十"
|
||||
let starts = "一二"
|
||||
let ends = "九十"
|
||||
let contains = "五六"
|
||||
let notContains = "百"
|
||||
|
||||
printfn "text = %A" text
|
||||
printfn "starts with %A: %A" starts (text.StartsWith(starts))
|
||||
printfn "starts with %A: %A" ends (text.StartsWith(ends))
|
||||
printfn "ends with %A: %A" ends (text.EndsWith(ends))
|
||||
printfn "ends with %A: %A" starts (text.EndsWith(starts))
|
||||
printfn "contains %A: %A" contains (text.Contains(contains))
|
||||
printfn "contains %A: %A" notContains (text.Contains(notContains))
|
||||
printfn "substring %A begins at position %d (zero-based)" contains (text.IndexOf(contains))
|
||||
let text2 = text + text
|
||||
printfn "text = %A" text2
|
||||
Seq.unfold (fun (n : int) ->
|
||||
let idx = text2.IndexOf(contains, n)
|
||||
if idx < 0 then None else Some (idx, idx+1)) 0
|
||||
|> Seq.iter (printfn "substring %A begins at position %d (zero-based)" contains)
|
||||
0
|
||||
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
|
||||
1
Task/String-matching/Factor/string-matching-1.factor
Normal file
1
Task/String-matching/Factor/string-matching-1.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"cheesecake" "cheese" head? ! t
|
||||
1
Task/String-matching/Factor/string-matching-2.factor
Normal file
1
Task/String-matching/Factor/string-matching-2.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"sec" "cheesecake" subseq? ! t
|
||||
1
Task/String-matching/Factor/string-matching-3.factor
Normal file
1
Task/String-matching/Factor/string-matching-3.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"cheesecake" "cake" tail? ! t
|
||||
1
Task/String-matching/Factor/string-matching-4.factor
Normal file
1
Task/String-matching/Factor/string-matching-4.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"sec" "cheesecake" subseq-start ! 4
|
||||
2
Task/String-matching/Factor/string-matching-5.factor
Normal file
2
Task/String-matching/Factor/string-matching-5.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
USE: regexp
|
||||
"Mississippi" "iss" <regexp> all-matching-slices [ from>> ] map ! { 1 4 }
|
||||
14
Task/String-matching/Falcon/string-matching.falcon
Normal file
14
Task/String-matching/Falcon/string-matching.falcon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/* created by Aykayayciti Earl Lamont Montgomery
|
||||
April 9th, 2018 */
|
||||
|
||||
s1 = "Naig Ialocin Olracnaig"
|
||||
s2 = "Naig"
|
||||
|
||||
var = s1.startsWith(s2) ? s1 + " starts with " + s2 : s1 + " does not start with " + s2
|
||||
> var
|
||||
|
||||
s2 = "loc"
|
||||
var = s2 in s1 ? @ "$s1 contains $s2" : @ "$s1 does not contain $s2"
|
||||
> var
|
||||
|
||||
> s1.endsWith(s2) ? @ "s1 ends with $s2" : @ "$s1 does not end with $s2"
|
||||
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
|
||||
40
Task/String-matching/Fortran/string-matching-1.f
Normal file
40
Task/String-matching/Fortran/string-matching-1.f
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
SUBROUTINE STARTS(A,B) !Text A starts with text B?
|
||||
CHARACTER*(*) A,B
|
||||
IF (INDEX(A,B).EQ.1) THEN !Searches A to find B.
|
||||
WRITE (6,*) ">",A,"< starts with >",B,"<"
|
||||
ELSE
|
||||
WRITE (6,*) ">",A,"< does not start with >",B,"<"
|
||||
END IF
|
||||
END SUBROUTINE STARTS
|
||||
|
||||
SUBROUTINE HAS(A,B) !Text B appears somewhere in text A?
|
||||
CHARACTER*(*) A,B
|
||||
INTEGER L
|
||||
L = INDEX(A,B) !The first position in A where B matches.
|
||||
IF (L.LE.0) THEN
|
||||
WRITE (6,*) ">",A,"< does not contain >",B,"<"
|
||||
ELSE
|
||||
WRITE (6,*) ">",A,"< contains a >",B,"<, offset",L
|
||||
END IF
|
||||
END SUBROUTINE HAS
|
||||
|
||||
SUBROUTINE ENDS(A,B) !Text A ends with text B.
|
||||
CHARACTER*(*) A,B
|
||||
INTEGER L
|
||||
L = LEN(A) - LEN(B) !Find the tail end of A that B might match.
|
||||
IF (L.LT.0) THEN !Dare not use an OR, because of full evaluation risks.
|
||||
WRITE (6,*) ">",A,"< is too short to end with >",B,"<" !Might as well have a special message.
|
||||
ELSE IF (A(L + 1:L + LEN(B)).NE.B) THEN !Otherwise, it is safe to look.
|
||||
WRITE (6,*) ">",A,"< does not end with >",B,"<"
|
||||
ELSE
|
||||
WRITE (6,*) ">",A,"< ends with >",B,"<"
|
||||
END IF
|
||||
END SUBROUTINE ENDS
|
||||
|
||||
CALL STARTS("This","is")
|
||||
CALL STARTS("Theory","The")
|
||||
CALL HAS("Bananas","an")
|
||||
CALL ENDS("Banana","an")
|
||||
CALL ENDS("Banana","na")
|
||||
CALL ENDS("Brief","Much longer")
|
||||
END
|
||||
62
Task/String-matching/Fortran/string-matching-2.f
Normal file
62
Task/String-matching/Fortran/string-matching-2.f
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
!-----------------------------------------------------------------------
|
||||
!Main program string_matching
|
||||
!-----------------------------------------------------------------------
|
||||
program string_matching
|
||||
implicit none
|
||||
character(len=*), parameter :: fmt= '(I0)'
|
||||
write(*,fmt) starts("this","is")
|
||||
write(*,fmt) starts("theory","the")
|
||||
write(*,fmt) has("bananas","an")
|
||||
write(*,fmt) ends("banana","an")
|
||||
write(*,fmt) ends("banana","na")
|
||||
write(*,fmt) ends("brief","much longer")
|
||||
|
||||
contains
|
||||
! Determining if the first string starts with second string
|
||||
function starts(string1, string2) result(answer)
|
||||
implicit none
|
||||
character(len=*), intent(in) :: string1
|
||||
character(len=*), intent(in) :: string2
|
||||
integer :: answer
|
||||
answer = 0
|
||||
if(len(string2)>len(string1)) return
|
||||
if(string1(1:len(string2))==string2) answer = 1
|
||||
end function starts
|
||||
! Determining if the first string contains the second string at any location
|
||||
function has(string1, string2) result(answer)
|
||||
implicit none
|
||||
character(len=*), intent(in) :: string1
|
||||
character(len=*), intent(in) :: string2
|
||||
character(len=:),allocatable :: temp
|
||||
integer :: answer, add
|
||||
character(len=*), parameter :: fmt= '(A6,X,I0)'
|
||||
answer = 0
|
||||
add = 0
|
||||
if(len(string2)>len(string1)) return
|
||||
answer = index(string1, string2)
|
||||
if(answer==0) return
|
||||
! Print the location of the match for part 2
|
||||
write(*,fmt) " at ", answer
|
||||
! Handle multiple occurrences of a string for part 2.
|
||||
add = answer
|
||||
temp = string1(answer+1:)
|
||||
do while(answer>0)
|
||||
answer = index(temp, string2)
|
||||
add = add + answer
|
||||
if(answer>0) write(*,fmt) " at ", add
|
||||
! deallocate(temp)
|
||||
temp = string1(add+1:) ! auto reallocation
|
||||
enddo
|
||||
answer = 1
|
||||
end function has
|
||||
! Determining if the first string ends with the second string
|
||||
function ends(string1, string2) result(answer)
|
||||
implicit none
|
||||
character(len=*), intent(in) :: string1
|
||||
character(len=*), intent(in) :: string2
|
||||
integer :: answer
|
||||
answer = 0
|
||||
if(len(string2)>len(string1)) return
|
||||
if(string1(len(string1)-len(string2)+1:)==string2) answer = 1
|
||||
end function ends
|
||||
end program string_matching
|
||||
26
Task/String-matching/FreeBASIC/string-matching.basic
Normal file
26
Task/String-matching/FreeBASIC/string-matching.basic
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Dim As String s1 = "abracadabra"
|
||||
Dim As String s2 = "abra"
|
||||
Print "First string : "; s1
|
||||
Print "Second string : "; s2
|
||||
Print
|
||||
Print "First string begins with second string : "; CBool(s2 = Left(s1, Len(s2)))
|
||||
Dim As Integer i1 = Instr(s1, s2)
|
||||
Dim As Integer i2
|
||||
Print "First string contains second string : ";
|
||||
If i1 Then
|
||||
Print "at index"; i1;
|
||||
i2 = Instr(i1 + Len(s2), s1, s2)
|
||||
If i2 Then
|
||||
Print " and at index"; i2
|
||||
Else
|
||||
Print
|
||||
End If
|
||||
Else
|
||||
Print "false";
|
||||
End If
|
||||
Print "First string ends with second string : "; CBool(s2 = Right(s1, Len(s2)))
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
75
Task/String-matching/FutureBasic/string-matching.basic
Normal file
75
Task/String-matching/FutureBasic/string-matching.basic
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
window 1, @"String matching", (0,0,650,360)
|
||||
|
||||
void local fn DoIt
|
||||
CFStringRef s1, s2
|
||||
CFRange range
|
||||
|
||||
s1 = @"alphabravocharlie"
|
||||
s2 = @"alpha"
|
||||
if ( fn StringHasPrefix( s1, s2 ) )
|
||||
print @"\"";s1;@"\" starts with \"";s2;@"\""
|
||||
else
|
||||
print @"\"";s1;@"\" does not start with \"";s2;@"\""
|
||||
end if
|
||||
|
||||
print
|
||||
|
||||
s2 = @"bravo"
|
||||
if ( fn StringHasPrefix( s1, s2 ) )
|
||||
print @"\"";s1;@"\" starts with \"";s2;@"\""
|
||||
else
|
||||
print @"\"";s1;@"\" does not start with \"";s2;@"\""
|
||||
end if
|
||||
|
||||
print
|
||||
|
||||
range = fn StringRangeOfString( s1, s2 )
|
||||
if ( range.location != NSNotFound )
|
||||
print @"\"";s1;@"\" contains \"";s2;@"\" at location ";(range.location)
|
||||
else
|
||||
print @"\"";s1;@"\" does not contain \"";s2;@"\""
|
||||
end if
|
||||
|
||||
print
|
||||
|
||||
s2 = @"delta"
|
||||
range = fn StringRangeOfString( s1, s2 )
|
||||
if ( range.location != NSNotFound )
|
||||
print @"\"";s1;@"\" contains \"";s2;@"\" at location ";(range.location)
|
||||
else
|
||||
print @"\"";s1;@"\" does not contain \"";s2;@"\""
|
||||
end if
|
||||
|
||||
print
|
||||
|
||||
s2 = @"charlie"
|
||||
if ( fn StringHasSuffix( s1, s2 ) )
|
||||
print @"\"";s1;@"\" ends with \"";s2;@"\""
|
||||
else
|
||||
print @"\"";s1;@"\" does not end with \"";s2;@"\""
|
||||
end if
|
||||
|
||||
print
|
||||
|
||||
s2 = @"alpha"
|
||||
if ( fn StringHasSuffix( s1, s2 ) )
|
||||
print @"\"";s1;@"\" ends with \"";s2;@"\""
|
||||
else
|
||||
print @"\"";s1;@"\" does not end with \"";s2;@"\""
|
||||
end if
|
||||
|
||||
print
|
||||
|
||||
s1 = @"alpha delta charlie delta echo delta futurebasic"
|
||||
s2 = @"delta"
|
||||
range = fn StringRangeOfString( s1, s2 )
|
||||
while ( range.location != NSNotFound )
|
||||
print @"\"";s1;@"\" contains \"";s2;@"\" at location ";(range.location)
|
||||
range.location++
|
||||
range = fn StringRangeOfStringWithOptionsInRange( s1, s2, 0, fn CFRangeMake( range.location, len(s1)-range.location ) )
|
||||
wend
|
||||
end fn
|
||||
|
||||
fn DoIt
|
||||
|
||||
HandleEvents
|
||||
14
Task/String-matching/GDScript/string-matching.gd
Normal file
14
Task/String-matching/GDScript/string-matching.gd
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
@tool
|
||||
extends Node
|
||||
|
||||
@export var first_string: String
|
||||
@export var second_string: String
|
||||
|
||||
@export var starts_with: bool:
|
||||
get: return first_string.begins_with(second_string)
|
||||
|
||||
@export var contains: bool:
|
||||
get: return first_string.contains(second_string)
|
||||
|
||||
@export var ends_with: bool:
|
||||
get: return first_string.ends_with(second_string)
|
||||
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 + "'");
|
||||
}
|
||||
}
|
||||
9
Task/String-matching/Gambas/string-matching.gambas
Normal file
9
Task/String-matching/Gambas/string-matching.gambas
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
Public Sub Main()
|
||||
Dim sString1 As String = "Hello world"
|
||||
Dim sString2 As String = "Hello"
|
||||
|
||||
Print sString1 Begins Left(sString2, 5) 'Determine if the first string starts with second string
|
||||
If InStr(sString1, sString2) Then Print "True" Else Print "False" 'Determine if the first string contains the second string at any location
|
||||
Print sString1 Ends Left(sString2, 5) 'Determine if the first string ends with the second string
|
||||
|
||||
End
|
||||
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.hs
Normal file
12
Task/String-matching/Haskell/string-matching.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/Icon/string-matching.icon
Normal file
10
Task/String-matching/Icon/string-matching.icon
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
|
||||
2
Task/String-matching/Java/string-matching-1.java
Normal file
2
Task/String-matching/Java/string-matching-1.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
String string = "string matching";
|
||||
String suffix = "ing";
|
||||
32
Task/String-matching/Java/string-matching-10.java
Normal file
32
Task/String-matching/Java/string-matching-10.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
public class JavaApplication6 {
|
||||
public static void main(String[] args) {
|
||||
String strOne = "complexity";
|
||||
String strTwo = "udacity";
|
||||
stringMatch(strOne, strTwo);
|
||||
}
|
||||
|
||||
public static void stringMatch(String one, String two) {
|
||||
boolean match = false;
|
||||
if (one.charAt(0) == two.charAt(0)) {
|
||||
System.out.println(match = true); // returns true
|
||||
} else {
|
||||
System.out.println(match); // returns false
|
||||
}
|
||||
for (int i = 0; i < two.length(); i++) {
|
||||
int temp = i;
|
||||
for (int x = 0; x < one.length(); x++) {
|
||||
if (two.charAt(temp) == one.charAt(x)) {
|
||||
System.out.println(match = true); //returns true
|
||||
i = two.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
int num1 = one.length() - 1;
|
||||
int num2 = two.length() - 1;
|
||||
if (one.charAt(num1) == two.charAt(num2)) {
|
||||
System.out.println(match = true);
|
||||
} else {
|
||||
System.out.println(match = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Task/String-matching/Java/string-matching-2.java
Normal file
1
Task/String-matching/Java/string-matching-2.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
string.startsWith(suffix)
|
||||
1
Task/String-matching/Java/string-matching-3.java
Normal file
1
Task/String-matching/Java/string-matching-3.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
string.substring(0, suffix.length()).equals(suffix)
|
||||
1
Task/String-matching/Java/string-matching-4.java
Normal file
1
Task/String-matching/Java/string-matching-4.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
string.contains(suffix)
|
||||
1
Task/String-matching/Java/string-matching-5.java
Normal file
1
Task/String-matching/Java/string-matching-5.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
string.indexOf(suffix) != -1
|
||||
1
Task/String-matching/Java/string-matching-6.java
Normal file
1
Task/String-matching/Java/string-matching-6.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
string.endsWith(suffix);
|
||||
1
Task/String-matching/Java/string-matching-7.java
Normal file
1
Task/String-matching/Java/string-matching-7.java
Normal file
|
|
@ -0,0 +1 @@
|
|||
string.substring(string.length() - suffix.length()).equals(suffix)
|
||||
6
Task/String-matching/Java/string-matching-8.java
Normal file
6
Task/String-matching/Java/string-matching-8.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
int indexOf;
|
||||
int offset = 0;
|
||||
while ((indexOf = string.indexOf(suffix, offset)) != -1) {
|
||||
System.out.printf("'%s' @ %d to %d%n", suffix, indexOf, indexOf + suffix.length() - 1);
|
||||
offset = indexOf + 1;
|
||||
}
|
||||
7
Task/String-matching/Java/string-matching-9.java
Normal file
7
Task/String-matching/Java/string-matching-9.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."))
|
||||
3
Task/String-matching/Jq/string-matching-1.jq
Normal file
3
Task/String-matching/Jq/string-matching-1.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# startswith/1 is boolean:
|
||||
"abc" | startswith("ab")
|
||||
#=> true
|
||||
6
Task/String-matching/Jq/string-matching-2.jq
Normal file
6
Task/String-matching/Jq/string-matching-2.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# index/1 returns the index or null,
|
||||
# so the jq test "if index(_) then ...." can be used
|
||||
# without any type conversion.
|
||||
|
||||
"abcd" | index( "bc")
|
||||
#=> 1
|
||||
3
Task/String-matching/Jq/string-matching-3.jq
Normal file
3
Task/String-matching/Jq/string-matching-3.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# endswith/1 is also boolean:
|
||||
"abc" | endswith("bc")
|
||||
#=> true
|
||||
5
Task/String-matching/Jq/string-matching-4.jq
Normal file
5
Task/String-matching/Jq/string-matching-4.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"abc" | test( "^ab")
|
||||
|
||||
"abcd" | test("bc")
|
||||
|
||||
"abcd" | test("cd$")
|
||||
6
Task/String-matching/Jq/string-matching-5.jq
Normal file
6
Task/String-matching/Jq/string-matching-5.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# In jq 1.4 or later:
|
||||
jq -n '"abcdabcd" | indices("bc")'
|
||||
[
|
||||
1,
|
||||
5
|
||||
]
|
||||
3
Task/String-matching/Jq/string-matching-6.jq
Normal file
3
Task/String-matching/Jq/string-matching-6.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$ jq -n '"abcdabcd" | match("bc"; "g") | .offset'
|
||||
1
|
||||
5
|
||||
7
Task/String-matching/Julia/string-matching.julia
Normal file
7
Task/String-matching/Julia/string-matching.julia
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
startswith("abcd","ab") #returns true
|
||||
findfirst("ab", "abcd") #returns 1:2, indices range where string was found
|
||||
endswith("abcd","zn") #returns false
|
||||
match(r"ab","abcd") != Nothing #returns true where 1st arg is regex string
|
||||
for r in eachmatch(r"ab","abab")
|
||||
println(r.offset)
|
||||
end #returns 1, then 3 matching the two starting indices where the substring was found
|
||||
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
|
||||
16
Task/String-matching/Kotlin/string-matching.kotlin
Normal file
16
Task/String-matching/Kotlin/string-matching.kotlin
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
fun main() {
|
||||
val s1 = "abracadabra"
|
||||
val s2 = "abra"
|
||||
println("$s1 begins with $s2: ${s1.startsWith(s2)}")
|
||||
println("$s1 ends with $s2: ${s1.endsWith(s2)}")
|
||||
val b = s2 in s1
|
||||
if (b) {
|
||||
print("$s1 contains $s2 at these indices: ")
|
||||
// can use indexOf to get first index or lastIndexOf to get last index
|
||||
// to get ALL indices, use a for loop or Regex
|
||||
println(
|
||||
s2.toRegex(RegexOption.LITERAL).findAll(s1).joinToString { it.range.start.toString() }
|
||||
)
|
||||
}
|
||||
else println("$s1 does not contain $2.")
|
||||
}
|
||||
69
Task/String-matching/Ksh/string-matching.ksh
Normal file
69
Task/String-matching/Ksh/string-matching.ksh
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#!/bin/ksh
|
||||
exec 2> /tmp/String_matching.err
|
||||
|
||||
# String matching
|
||||
# # 1. Determine if the first string starts with second string.
|
||||
# # 2. Determine if the first string contains the second string at any location
|
||||
# # 3. Determine if the first string ends with the second string
|
||||
# # 4. Print the location of the match for part 2
|
||||
# # 5. Handle multiple occurrences of a string for part 2
|
||||
|
||||
# # Variables:
|
||||
#
|
||||
typeset -a bounds=( [0]="no Match" [1]="Starts with" [255]="Ends with" )
|
||||
|
||||
typeset -a string=( "Hello" "hello world" "William Williams" "Yabba dabba do" )
|
||||
typeset -a substr=( "Hell" "Do" "abba" "Will" "orld" )
|
||||
|
||||
# # Functions:
|
||||
#
|
||||
# # Function _bounds(str, substr) - return 1 for starts with 255 for endswith
|
||||
#
|
||||
function _bounds {
|
||||
typeset _str ; _str="$1"
|
||||
typeset _sub ; _sub="$2"
|
||||
|
||||
typeset _FALSE _STARTS _ENDS ; integer _FALSE=0 _STARTS=1 _ENDS=255
|
||||
|
||||
[[ "${_str}" == "${_sub}"* ]] && return ${_STARTS}
|
||||
[[ "${_str}" == *"${_sub}" ]] && return ${_ENDS}
|
||||
return ${_FALSE}
|
||||
}
|
||||
|
||||
# # Function _contains(str, substr) - return 0 no match arr[pos1 ... posn]
|
||||
#
|
||||
function _contains {
|
||||
typeset _str ; _str="$1"
|
||||
typeset _sub ; _sub="$2"
|
||||
typeset _arr ; nameref _arr="$3"
|
||||
|
||||
typeset _FALSE _TRUE _i _match _buff ; integer _FALSE=0 _TRUE=1 _i _match
|
||||
|
||||
[[ "${_str}" != *"${_sub}"* ]] && return ${_FALSE}
|
||||
|
||||
for ((_i=0; _i<=${#_str}-${#_sub}; _i++)); do
|
||||
_buff=${_str:${_i}:$((${#_str}-_i))}
|
||||
[[ ${_buff} != ${_buff#${_sub}} ]] && _arr+=( $(( _i+1 )) )
|
||||
done
|
||||
return ${_TRUE}
|
||||
}
|
||||
|
||||
######
|
||||
# main #
|
||||
######
|
||||
|
||||
integer i j rc
|
||||
typeset -a posarr
|
||||
|
||||
for ((i=0; i<${#string[*]}; i++)); do
|
||||
for ((j=0; j<${#substr[*]}; j++)); do
|
||||
_bounds "${string[i]}" "${substr[j]}" ; rc=$?
|
||||
print "${string[i]} ${bounds[rc]} ${substr[j]}"
|
||||
|
||||
_contains "${string[i]}" "${substr[j]}" posarr ; rc=$?
|
||||
((! rc)) && print "${string[i]} ${substr[j]} ${bounds[rc]}es" && continue
|
||||
|
||||
print "${string[i]} + ${substr[j]} ${#posarr[*]} matches at ${posarr[*]}"
|
||||
unset posarr ; typeset -a posarr
|
||||
done
|
||||
done
|
||||
43
Task/String-matching/Lambdatalk/string-matching.lambdatalk
Normal file
43
Task/String-matching/Lambdatalk/string-matching.lambdatalk
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
{def S.in
|
||||
{def S.in.r {lambda {:c :w :i :n}
|
||||
{if {= :i :n}
|
||||
then -1
|
||||
else {if {W.equal? :c {W.get :i :w}}
|
||||
then :i
|
||||
else {S.in.r :c :w {+ :i 1} :n}}}}}
|
||||
{lambda {:c :w}
|
||||
{S.in.r :c :w 0 {W.length :w}}}}
|
||||
-> S.in
|
||||
|
||||
|
||||
|
||||
{def startswith
|
||||
{lambda {:w1 :w2}
|
||||
{= {S.in _ {S.replace :w2 by _ in :w1}} 0}}}
|
||||
-> startswith
|
||||
|
||||
{def endswith
|
||||
{lambda {:w1 :w2}
|
||||
{= {S.in _ {S.replace :w2 by _ in :w1}}
|
||||
{- {W.length :w1} {W.length :w2}}}}}
|
||||
-> endswith
|
||||
|
||||
{def isin
|
||||
{lambda {:w1 :w2}
|
||||
{S.in _ {S.replace :w2 by _ in :w1}}}}
|
||||
-> isin
|
||||
|
||||
{startswith nabuchodonosor nabu}
|
||||
-> true
|
||||
{startswith nabuchodonosor abu}
|
||||
-> false
|
||||
|
||||
{endswith nabuchodonosor sor}
|
||||
-> true
|
||||
{endswith nabuchodonosor oso}
|
||||
-> false
|
||||
|
||||
{isin nabuchodonosor oso}
|
||||
-> 10 // is in at 10
|
||||
{isin nabuchodonosor xyz}
|
||||
-> -1 // is not in
|
||||
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
|
||||
14
Task/String-matching/Lasso/string-matching.lasso
Normal file
14
Task/String-matching/Lasso/string-matching.lasso
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
local(
|
||||
a = 'a quick brown peanut jumped over a quick brown fox',
|
||||
b = 'a quick brown'
|
||||
)
|
||||
|
||||
//Determining if the first string starts with second string
|
||||
#a->beginswith(#b) // true
|
||||
|
||||
//Determining if the first string contains the second string at any location
|
||||
#a >> #b // true
|
||||
#a->contains(#b) // true
|
||||
|
||||
//Determining if the first string ends with the second string
|
||||
#a->endswith(#b) // false
|
||||
30
Task/String-matching/Liberty-BASIC/string-matching.basic
Normal file
30
Task/String-matching/Liberty-BASIC/string-matching.basic
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
|
||||
22
Task/String-matching/Lingo/string-matching.lingo
Normal file
22
Task/String-matching/Lingo/string-matching.lingo
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
a = "Hello world!"
|
||||
b = "Hello"
|
||||
|
||||
-- Determining if the first string starts with second string
|
||||
put a starts b
|
||||
-- 1
|
||||
|
||||
-- Determining if the first string contains the second string at any location
|
||||
put a contains b
|
||||
-- 1
|
||||
|
||||
-- Determining if the first string ends with the second string
|
||||
put a.char[a.length-b.length+1..a.length] = b
|
||||
-- 0
|
||||
|
||||
b = "world!"
|
||||
put a.char[a.length-b.length+1..a.length] = b
|
||||
-- 1
|
||||
|
||||
-- Print the location of the match for part 2
|
||||
put offset(b, a)
|
||||
-- 7
|
||||
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 ) )
|
||||
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
|
||||
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
|
||||
37
Task/String-matching/MIPS-Assembly/string-matching-1.mips
Normal file
37
Task/String-matching/MIPS-Assembly/string-matching-1.mips
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
InString:
|
||||
;input: $a0 = ptr to string 1
|
||||
; $a1 = ptr to string 2
|
||||
; assumes len($a1) <= len($a0)
|
||||
;out: $v0 = zero-based index where the second string is placed in the first.
|
||||
|
||||
;clobbers: $t0,$t1
|
||||
subiu sp,sp,4 ;set up a stack frame of 4 bytes.
|
||||
sw $a1,(sp)
|
||||
li $v0,0
|
||||
InString_again:
|
||||
lbu $t0,($a0)
|
||||
nop
|
||||
beqz $t0,InString_terminated
|
||||
nop
|
||||
|
||||
lbu $t1,($a1)
|
||||
nop
|
||||
beqz $t1,InString_terminated
|
||||
nop
|
||||
|
||||
bne $t0,$t1,InString_noMatch
|
||||
nop
|
||||
b InString_overhead
|
||||
addiu $a1,1
|
||||
|
||||
InString_noMatch:
|
||||
lw $a1,(sp) ;reset the substring pointer if the letters don't match
|
||||
addiu $v0,1 ;load delay slot
|
||||
InString_overhead:
|
||||
addiu $a0,1
|
||||
b InString_Again
|
||||
nop
|
||||
InString_terminated:
|
||||
addiu sp,sp,4
|
||||
jr ra
|
||||
nop
|
||||
41
Task/String-matching/MIPS-Assembly/string-matching-2.mips
Normal file
41
Task/String-matching/MIPS-Assembly/string-matching-2.mips
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
main:
|
||||
la $a0,MyString
|
||||
la $a1,Test1 ;this code was recompiled 5 times, testing a different string each time.
|
||||
jal InString
|
||||
nop
|
||||
|
||||
jal Monitor
|
||||
nop
|
||||
|
||||
|
||||
shutdown:
|
||||
nop ;Project 64 will detect an infinite loop and close the ROM if I don't have this nop here.
|
||||
b shutdown
|
||||
nop
|
||||
|
||||
|
||||
MyString: ;this was loaded into $a0
|
||||
.ascii "abcdefghijklmnopqrstuvwxyz"
|
||||
.byte 0
|
||||
.align 4
|
||||
;each of these was loaded into $a1 individually for testing
|
||||
Test1:
|
||||
.ascii "abc" ;InString returned 0
|
||||
.byte 0
|
||||
.align 4
|
||||
Test2:
|
||||
.ascii "xyz" ;InString returned 0x17 (decimal 23)
|
||||
.byte 0
|
||||
.align 4
|
||||
Test3:
|
||||
.ascii "def" ;InString returned 3
|
||||
.byte 0
|
||||
.align 4
|
||||
Test4:
|
||||
.ascii "z",0 ;InString returned 0x19 (decimal 25)
|
||||
.byte 0
|
||||
.align 4
|
||||
Test5:
|
||||
.ascii "1",0 ;InString returned 0x1A (decimal 26)
|
||||
.byte 0
|
||||
.align 4
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue