For a given source string and a target string, should output the first index (from 0) of target string in sour Ce string.
If target does not exist in source, just return -1
.
If Source = "source"
and target = "target"
, return -1
.
If Source = "abcdabcdefg"
and target = "bcd"
, return 1
.
classSolution {/*** Returns A index to the first occurrence of target in source, * or-1 If Target was not part of source. * @paramsource string to is scanned. * @paramtarget string containing the sequence of characters to match. */ Public intstrStr (string source, string target) {//Write your code here if(Source = =NULL|| target = =NULL) return-1; if(source.length () = = 0 && target.length () = = 0) return0; if(target.length () = = 0) return0; intSize1 =source.length (); intSize2 =target.length (); if(Size1 <size2)return-1; for(inti = 0; I <= size1-size2; i++){ if(Source.charat (i) = = Target.charat (0)){ intj = 0; for(; J < Size2; J + +){ if(Source.charat (i + j)! =Target.charat (j)) Break; } if(J = =size2)returni; } } return-1; }}
Lintcode-easy-strstr