@
fcantoro
Please don't go on holy language wars. OP is using C.
@
zinat
Your
strtrmm() function should not be asking the user for input. That should be happening in
main().
Lines 5 and 20 don't match (but they should).
I always recommend putting
main() last, but everyone seems dead-set on zillions of prototypes at the top of the file to track what follows in the file... so do what your instructor asks.
For a homework assignment, I can't give you grief, but in the future, don't use
gets(). It's an evil function and should never have existed. (That's not just my opinion, BTW, but the opinion of experts. The function is
dangerous.)
In any case, your
strtrmm() function needs to know:
- the string to be searched
- the string to find
- (optionally) the index to begin searching in (the string to be searched)
I am unsure, looking at your code, what you are trying to return. It should be
one of the following:
- the address in (the string to be searched) of (the string to find)
- the index into (the string to be searched) of (the string to find)
- the number of times in (the string to be searched) that (the string to find) is found
If you opt to return the address of the find, remember that the address is somewhere inside the existing string (array), so your function can look like this:
char* strtrmm( char* searchme, char* findme );
Remember that the result will be somewhere inside 'searchme', so if you were to call the function like this:
1 2 3
|
char* s = "Hello world";
char* t = strtrmm( s, s );
|
Then the result would be (t == s).
Likewise, for:
1 2 3
|
char* s = "Hello world";
char* t = strtrmm( s, "world" );
|
the result would be (t == (s+6)).
In any case, you shouldn't be printing straight from the result, since you should not modify the 'searchme' string. For example:
1 2
|
// this:
printf( "%s\n", strtrmm( "Hello world", "ello" );
|
// produces:
ello world |
It found "ello", but the string ends after the 'd' in "world". You would need to copy the substring out -- BUT, it is unnecessary anyway, as you already have a copy of the substring.
1 2 3 4 5 6 7 8 9
|
char* a = "Hello world";
char* b = "ello";
char* t = strtrmm( a, b );
if (t != NULL)
printf( "Found %s\n", b );
else
puts( "Not found!" );
|
Notice also that when 'findme' is not found, the function should return something useful, like
NULL or the address of the null at the end of the string.
Your actual assignment is unclear, so I hope these ruminations help.