Author |
Message |
demirtun #1 / 8
|
 string search
i want to rewrite the function strtsr in the standard library, ...err...ok its a homework :) But i have a code (the golden rule !). Actually it is not the same function, this function returns an integer indicating the starting location of the first occurence of the second parameter string within the first parameter string. I wrote this (below) function but it does not work correctly. When the first string is "demirtunc" and the second is "tunc", it works, but if the second string is "demir" does not. What is wrong with this code ? thanks int strSearch(char s1[], char s2[]) { int len1, len2, i, j1, j2; len1 = strlen(s1); len2 = strlen(s2); for(i=0; i+len2 <= len1; i++) for(j1=i, j2=0; j2 <= len2 && s1[j1] == s2[j2]; j1++, j2++) if(j2==len2) return i+1; return -1; Quote: }
-- C-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html C-Library: http://www.*-*-*.com/
|
Sun, 27 Jun 2004 21:26:29 GMT |
|
 |
Dan Cerna #2 / 8
|
 string search
Quote: > i want to rewrite the function strtsr in the standard library, ...err...ok > its a homework :) But i have a code (the golden rule !). > Actually it is not the same function, this function returns an integer > indicating the starting location of the first occurence of the second > parameter string within the first parameter string. > I wrote this (below) function but it does not work correctly. When the first > string is "demirtunc" and the second is "tunc", it works, but if the second > string is "demir" does not. What is wrong with this code ? thanks > int strSearch(char s1[], char s2[]) > { > int len1, len2, i, j1, j2; > len1 = strlen(s1); > len2 = strlen(s2); > for(i=0; i+len2 <= len1; i++) > for(j1=i, j2=0; j2 <= len2 && s1[j1] == s2[j2]; j1++, j2++) > if(j2==len2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^ if (j2 == len2 - 1) Quote: > return i+1; > return -1; > } > -- > C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html > C-Library: http://www.dinkumware.com/htm_cl/index.html
Dan
|
Sun, 27 Jun 2004 23:34:34 GMT |
|
 |
Zoran Cutur #3 / 8
|
 string search
... Quote: > I wrote this (below) function but it does not work correctly. When the first > string is "demirtunc" and the second is "tunc", it works, but if the second > string is "demir" does not. What is wrong with this code ? thanks > int strSearch(char s1[], char s2[]) > { > int len1, len2, i, j1, j2; > len1 = strlen(s1); > len2 = strlen(s2); > for(i=0; i+len2 <= len1; i++) > for(j1=i, j2=0; j2 <= len2 && s1[j1] == s2[j2]; j1++, j2++) > if(j2==len2) > return i+1; > return -1; > }
Your try wasn't to bad, but you have made a few off-bye-one errors. You have to remember, when you calculate the length of the string, and add this value to the pointer pointing to the beginning, you get a pointer to the '\0'-character. But you don't want to consume the '\0'-character in this function. Here are my corrections: int strSearch(char s1[], char s2[]) { int len1, len2, i, j1, j2; len1 = strlen(s1); len2 = strlen(s2); for(i=0; i+len2 < len1; i++) /* < len1 instead of <= len1 */ for(j1=i, j2=0; j2 < len2 && s1[j1] == s2[j2]; j1++, j2++) /* < len2 instead of <= len2 */ if(j2==len2-1) /* == len2 - 1 instead of == len2 */ return i+1; return -1; Quote: }
--
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Sun, 27 Jun 2004 23:27:07 GMT |
|
 |
CBFalcone #4 / 8
|
 string search
Quote:
> i want to rewrite the function strtsr in the standard library, ...err...ok > its a homework :) But i have a code (the golden rule !). > Actually it is not the same function, this function returns an integer > indicating the starting location of the first occurence of the second > parameter string within the first parameter string. > I wrote this (below) function but it does not work correctly. When the first > string is "demirtunc" and the second is "tunc", it works, but if the second > string is "demir" does not. What is wrong with this code ? thanks > int strSearch(char s1[], char s2[]) > { > int len1, len2, i, j1, j2; > len1 = strlen(s1); > len2 = strlen(s2); > for(i=0; i+len2 <= len1; i++) > for(j1=i, j2=0; j2 <= len2 && s1[j1] == s2[j2]; j1++, j2++) > if(j2==len2) > return i+1; > return -1; > }
If s2 is "demir" len2 is 5, and s1[5] is 't'. Replace <= with <. Always check boundary conditions. Also change the if condition. Change the function name - you are invading the implementation namespace. --
Available for consulting/temporary embedded and systems. (Remove "XXXX" from reply address. yahoo works unmodified)
|
Mon, 28 Jun 2004 02:47:25 GMT |
|
 |
Simon Bibe #5 / 8
|
 string search
Quote:
> int strSearch(char s1[], char s2[])
Quote: > Change the function name - you are invading the > implementation namespace.
C99 7.26.11 Function names that begin with str, mem, or wcs and a lowercase letter may be added to the declarations in the <string.h> header. 'S' is not a lowercase letter. -- Simon.
|
Mon, 28 Jun 2004 03:18:49 GMT |
|
 |
Stephen Montgomery-Smit #6 / 8
|
 string search
Quote:
> i want to rewrite the function strtsr in the standard library, ...err...ok > its a homework :) But i have a code (the golden rule !). > Actually it is not the same function, this function returns an integer > indicating the starting location of the first occurence of the second > parameter string within the first parameter string. > I wrote this (below) function but it does not work correctly. When the first > string is "demirtunc" and the second is "tunc", it works, but if the second > string is "demir" does not. What is wrong with this code ? thanks
I changed the logic a bit from what you were intending, but: Quote: > int strSearch(char s1[], char s2[]) > { > int len1, len2, i, j1, j2; > len1 = strlen(s1); > len2 = strlen(s2);
for(i=0; i+len2 <= len1; i++) { for(j1=i, j2=0; j2 < len2 && s1[j1] == s2[j2]; j1++, j2++) /* do nothing */; if(j2==len2) return i; /* You had i+1 - it depends what you want, but I think my choice is more natural. */ } Quote: -- Stephen Montgomery-Smith
http://www.math.missouri.edu/~stephen
|
Mon, 28 Jun 2004 14:02:05 GMT |
|
 |
Micah Cowa #7 / 8
|
 string search
Quote:
> > int strSearch(char s1[], char s2[])
> > Change the function name - you are invading the > > implementation namespace. > C99 7.26.11 > Function names that begin with str, mem, or wcs and a lowercase letter may > be added > to the declarations in the <string.h> header.
True. However, in C90, external links may be case-insensitive, in which case it doesn't matter that the 'S' isn't lowercase. Micah
|
Mon, 28 Jun 2004 15:28:53 GMT |
|
 |
CBFalcone #8 / 8
|
 string search
Quote:
> > int strSearch(char s1[], char s2[])
> > Change the function name - you are invading the > > implementation namespace. > C99 7.26.11 > Function names that begin with str, mem, or wcs and a lowercase > letter may be added to the declarations in the <string.h> header. > 'S' is not a lowercase letter.
A direct hit. I am writhing in agony on the floor, hoisted by the petards of my own fallability. --
Available for consulting/temporary embedded and systems. (Remove "XXXX" from reply address. yahoo works unmodified)
|
Mon, 28 Jun 2004 16:27:18 GMT |
|
|
|