Problem in function sup

0

I'm here with a problem in this function int sup (char s1 [], char s2 []) , that calculates the largest suffix size of s1 that is prefixed to s2. / strong> For example, if you have supre ("cheat", "totality") it should return 4 , since the string "tota" is a suffix of "cheat" which is a prefix of "totality".

I tried to do this function with the strrev () function to compare character to character, but did not give great success ..

If there are any functions that help me, which one should I use? And it is possible to do without auxiliary functions?

    
asked by anonymous 21.03.2015 / 16:44

1 answer

2

This is a porbleminha that although simple, has no magic to solve: is a simple algorithm, that we can think 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 pairing string of the standard C library, and checking the characters directly. You can also write your functions if there are none.

But then - realize that it's an algorithm in "steps" - it's not a 3 or 4 line thing - why you'll have to at the very 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 the position N of the first character of S2 within S1;
  • If there is no N, return saying that the suffix is the empty string
  • create a counter i = 0;
  • checkpoint
  • copy the character S1 [N] to the response string S3
  • Increase 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 terminator in S3 and return S3 even if we reached the end of S2)
  • compare the character N + i in S2 with the character in position (0 + i) of S2;
  • If they are different, clear the string S3, and return an empty suffix (** but see note below)
  • return to "checkpoint"

Poronto - now you translate this to "C" - and notice that in the description in Portuguese comes naturally what will be a loop "for" or "while" in a 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 account. (for example: "cheating", and "rate" - he will find the first "t" of "cheat", but will stop the search on "o" - when the suffix is after the second "t") - or you repeat the search for "t", or - to start all , look for the first letter of S2 in S1, but from right to left - this second alternative is a lot easier, but I think you will not have a ready-made function in C's stdlib for this - then you'll do your own little function for that Believe me, it's worth it):

  • search for the letter X in S1 from the end:
  • make i equal the total length of S1
  • heckpoint check if the character S1 [i] is X - if yes, return i
  • decrement i
  • If i is less than zero, return i (and take advantage of return of -1 as an indication that the letter does not exist in S1)
  • go to checkpoint

- Ready now you already have where to start. It is not the purpose of the S.O. to give ready answers - so try to translate this to C.

    
27.05.2015 / 16:15