String matching: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 11: Line 11:
B) Handle multiple occurrences of a string for part 2.
B) Handle multiple occurrences of a string for part 2.
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{trans|python}}
{{trans|C}} - note: This specimen retains the original [[Character_matching.a68#C|C]] coding style.

{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}


{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - string in string is non-standard?}}
<lang algol68># define some appropriate OPerators #
<lang algol68># define some appropriate OPerators #
PRIO STARTSWITH = 5, ENDSWITH = 5;
PRIO STARTSWITH = 5, ENDSWITH = 5;

Revision as of 07:03, 15 October 2010

Task
String matching
You are encouraged to solve this task according to the task description, using any language you may know.

Given two strings, demonstrate the following 3 types of matchings:

  1. Determining if the first string starts with second string
  2. Determining if the first string contains the second string at any location
  3. Determining if the first string ends with the second string

Optional requirements: A) Print the location of the match for part 2 B) Handle multiple occurrences of a string for part 2.

ALGOL 68

Translation of: python
Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny

<lang algol68># 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 #

))</lang> Output:

TFFT         -1         +1         +3

Objective-C

<lang objc> [@"abcd" hasPrefix:@"ab"] //returns true [@"abcd" hasSuffix:@"zn"] //returns false int loc = [@"abab" rangeOfString:@"bb"].location //returns -1 loc = [@"abab" rangeOfString:@"ab"].location //returns 0 loc = [@"abab" rangeOfString:@"ab" options:0 range:NSMakeRange(loc+1, [@"abab" length]-(loc+1))].location //returns 2 </lang>

Python

<lang python> "abcd".startswith("ab") #returns true "abcd".endswith("zn") #returns false "bb" in "abab" #returns false "ab" in "abab" #returns true loc = "abab".find("bb") #returns -1 loc = "abab".find("ab") #returns 0 loc = "abab".find("ab",loc+1) #returns 2 </lang>