Re: I need help! Substrings

David MacQueen <[email protected]> Tue, 29 May 2007 17:53:04 -0500
Newsgroups gmane.comp.lang.sml.smlnj
Message-ID <[email protected]>
This is almost the same as the library function String.isSubstring,  
except for
the type.

Here is a hint (for a fairly simple, but not optimal solution).   
Define two auxiliary functions

val firstMatch : char * string * int -> int option
fun firstMatch(c: char, s: string, i: int) : int option =
        find the index of the first occurrence of character c in  
string s starting at index i.
	if c is found at index j in s, return SOME j, otherwise return NONE

val checkRest : string * int * string * int -> bool
fun checkRest(s1, s2, i) : bool =
       check that all the characters in string s1, starting at index  
1 (i.e. the second
       character) are matched by the corresponding characters in  
string s2, starting
       at index i.  return true if so, or false if there is a  
mismatch or s2 is exhausted
       before s1

Once you have these, it should be straightforward to put them  
together to define
the function subString function you want.

By the way, returning -1 for the failure case is bad style.  Much  
better to return
an int option (NONE or SOME) instead, as I suggested for firstMatch  
above.

Good luck.

Dave MacQueen

On May 29, 2007, at 3:41 PM, turbosol wrote:

>
> So i signed up for my last compscience class so i can graduate...  
> the class i
> needed was canceled so i was put into a more difficult class for  
> majors.
> Comp science is my minor... anyways, I am currently doing twice the  
> work i
> would normally do in half the time.  I am lost.  if anyone can help  
> me, i
> would appreciate it SOOO much.
>
> I need to write a function SubString : string * string -> int that  
> checks
> whether the first string is a substring of the second (case  
> sensitive).
> return the index position (starting from 0) if the string is a  
> substring or
> -1 otherwise.  for multiple occurances, just return the index of  
> the first
> appearance.  you may assume both input parameters are strings.
>
> subString("ab","abcdefg")=> 1
> subString("aaa","aaaa")=>0
> subString("bc","absge") =>-1
> subString("ab","cdabd") => 2
> -- 
> View this message in context: http://www.nabble.com/I-need-help%21-- 
> Substrings-tf3836438.html#a10862221
> Sent from the SML/NJ mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------- 
> ---
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Smlnj-list mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/smlnj-list
>


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/