This is a porbluff that although simple, has no magic to solve:
is a simple algorithm, which we can think of and understand how to solve "manually" - and the way is to understand the algorithm, and then transcribe to C.
You have a set of tools that are the functions for string amanipulation of the standard C library,and direct checking of the characters. You can also write your functions if there are none.
But then - notice that it’s a "step" algorithm - it’s not a 3 or 4-line thing - why are you going to have to at least find the starting point of the search, and compare the characters from there.
So the pseudo-algorithm in Portuguese could start like this:
- prepare an empty S3 response string.
- find if there is, and position N of the first character of S2 within S1;
- If there is no N, return saying the suffix is the empty string
- create a counter i = 0;
- checkpoint
- copy the S1[N] character to the S3 response string
- Increment the value of i
- if the character in S1[N+i] is the end of the string (character ' x00') - the answer is what we have in S3: add a string end mcarcador in S3 and return S3 (do the same if you reached the end of S2)
- compare the character N + i in S2 with the character at the position (0 + i) of S2;
- if they are different, reset the string S3, and return an empty suffix (** but see note below)
- return to "checkpoint"
Why -now you translate this to "C" - and notice that in the description in Portuguese emerges naturally what will be a "for" or "while' loop in programming language.
** - there is a catch in this algorithm there - if the first letter of the second word occurs more than once in the first word, the above description does not count. (for example: "cheating", and "tariff" -
he will find the first "t" of "cheat", but will stop the search in "o" - when the suffix is after the second "t") - or you repeat the search for "t", or- to start everything, look for the first letter of S2 in S1, but from right to left - this second alternative is much easier, but I don’t think you’ll have a function ready in stdlib of C for this - so you’ll do your own little function for this (believe me, it’s worth it):
- look for the letter X in S1 from the end:
- make i equal the total length of S1
- heckpoint`check that the character S1[i] is X - if yes, return i
- decreased
- if i is less than zero, retorn i (and take advantage of the
return of -1 as an indication that the letter does not exist in S1)
- go to checkpoint
--Ready now you have where to start. It is not the purpose of S.O. to give ready answers - so try to translate this to C.
Put what you have done so that we have an idea of what you are trying to do. Even if you have problems and are incomplete.
– Maniero
I made a recursive type solution that will test substrings of
s1
increasingly smaller: eg ... 1)"batota"
is not suffix to"totalidade"
2)"atota"
is not suffix to"totalidade"
3)"tota"
is suffix to"totalidade"
– pmg
@OP, you want a way to solve the problem or want tips to solve it?
– user2692