Anything that goes beyond scope is naughty.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Jobe, my very first reaction would be, why not write a generic function instead of restricting yourself with search strings of length 2 or 3.
goes out of bound. Isn't that bad coding? Its not bad coding, itswrong coding. How can you expect your program to perform properly and in the right manner if you are basing your programming logic on computations on areas of memory which don't belong to you ?
Also the trick which you use will soon turn out to be cumbersome if you are asked to do the same with 3 letters, 4 letters, n letters etc.
My thoughts: always look for generic patterns in the problem statement and if they don't incur additional overheads for that case, go ahead and implement them.
For eg here is one implementation which is possible. I haven't tried it but it should pretty much work.
// retuns the last position in the string where the match was found
// modifies the count parameter passed by reference which displays the
// number of times substring occurs.
int my_strstr( const char* str1, const char* str2, int* count )
{
int i = 0 ;
int len1 = strlen( str1 ) ;
int len2 = strlen( str2 ) ;
int pos = 0 ;
*count = 0 ; // initialize the counter
for( i = 0; i < len1; ++i )
{
if( strncmp( ( str1 + i ), str2, len2 ) == 0 )
{
(*count)++ ;
pos = i ;
}
}
return pos ;
}
int main( void )
{
int count ;
char str[] = "ababababa" ;
char search_str[] = "ab" ;
int match = my_strstr( str, search_str, &count ) ;
printf( "The last occurance of \'%s\' was found in \'%s\' at %d.",
str, search_str, match ) ;
printf( "\nThe count of \'%s\' is %d.", search_str, count ) ;
getchar( ) ;
return 0 ;
}
You can change the function so that you get it to return the char pointer rather than returning position or you can make the function do both by keeping its prototype as: char* my_strstr( const char* str1, const char* str2, int* count, int* pos )
which makes the function return char pointer to the substring found as well as modifies the parameters passed to it to reflect the count of substring as well as the position of the last substring.
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Hmmm, well, I came up with this int strLength = strlen(myArr)-1; and then in the loop for (int i = 0; i < strLength; i++) but I see where you're coming from :o
Thanks for the example, I'll see that I can find an equivalent to use in my program next week-end.
I would reverse the -1 and go with: int strLength = strlen(myArr); and for (int i = 0; i < strLength-1; i++)
The reason isstrLength actually represents the string length, which you can modify in the loop. And you can still use strLength elsewhere in your program if the need arises. By subtracting 1 first, it's really only useful for this particular part of the program.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Maybe the C++ version was supposed to use .find() ?
This is enough room for one character plus the terminating null:
char letters[2];
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Is there a better way to compare a character array with a cpp string?
There areother ways, but not necessarily better. For just two characters yours way is efficient. If you had to test 4 or more, a loop might be better.
There are also the C-string str???() functions and C++ strings compare methods. Either of those you can look into.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
HI Dave, long time no see, I see you lost your mod stick, good to see you here again though :)
My choice.
Here was my tinkerings:
int compareChar(char *myArr, char *mySecArr)
{
int count = 0;
size_t len = strlen(mySecArr);
while ( (mySecArr = strstr(myArr, mySecArr)) != NULL )
{
count++;
mySecArr += len;
}
return count;
}
int compareString(std::string myWord, char *mySecArr)
{
int count = 0;
std::string::size_type i = 0;
size_t len = strlen(mySecArr);
while ( (i = myWord.find(mySecArr, i)) != std::string::npos )
{
i += len;
count++;
}
return count;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Hmm...I think there is some typo mistakes in the above one posted by Mr. Dave.
int compareChar(char *myArr, char *mySecArr)
{
int count = 0;
size_t len = strlen(mySecArr);
// mySecArr should be myArr
while ( (mySecArr = strstr(myArr, mySecArr)) != NULL )
{
count++;
// mySecArr should be myArr
mySecArr += len;
}
return count;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Hmm...I think there is some typo mistakes in the above one posted by Mr. Dave.
Indeed. :o
int compareChar(char *myArr, char *mySecArr)
{
int count = 0;
size_t len = strlen(mySecArr);
while ( (myArr = strstr(myArr, mySecArr)) != NULL )
{
count++;
myArr += len;
}
return count;
}
Too many projects lying around for the same purpose; must consolidate.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314