strend
-
<p>Hello, voici un petit exercice, tiré du livre "The C Programming Language" (K&R).</p>
<p></p><p>En suivant ce prototype (vous pouvez changer le nom des paramètres) :</p>
<pre class="ipsCode prettyprint">
int strend(char *s, char *t)
</pre>
<p>Créez une fonction qui retourne 1 si la string <em>s</em> se termine par la string <em>t</em>,sinon, retourne 0.</p>
<p> </p>
<p>Exemples :</p>
<pre class="ipsCode prettyprint">
strend("ABCDavy","Davy"); // == 1
strend("Melinyel","oyel"); // == 0
strend("Microsoft","sofa"); // == 0
strend("Forum","rum"); // == 1
</pre>
<p>Je posterais ma solution mardi
</p> -
<p>OK, amusant, je part du principe qu'on a le droit aux fonctions de la libc, ce qui donnerais:</p>
<pre class="ipsCode prettyprint">
#include <stdlib.h>int strend(char *s, char *t)
{
char *tmp;tmp = s; while ((tmp = strstr(tmp, t)) != NULL); return (tmp != NULL && strlen(tmp) == strlen(t));}
</pre> -
<p>Voilà, j'ai edit mon code
</p> -
<p>Voici ma solution :</p>
<p></p><blockquote class="ipsStyle_spoiler" data-ipsspoiler="">
<p>Je sais pas ce qu'il c'est passer avec l'identation ...</p>
<pre class="ipsCode prettyprint">
int my_strend(const char *str, const char *ending)
{
int lStr = my_strlen(str);
int lEnd = my_strlen(ending);if(!lStr || !lEnd)
return 0;const char *iterator = str + lStr - lEnd;
int i = 0;
while(iterator[i] != '\0' && ending[i] != '\0')
{
if(iterator[i] != ending[i])
{
return 0;
}
i++;
}return 1;
}</pre>
<p>Itère de <em>str - taille d'str + taille de l'ending</em>, jusqu’à la fin de <em>str</em>, en comparant chaque caractère.</p>
<p></p></blockquote><p></p><p>Dommage qu'il n'y ai pas eu plus de participants
</p> -
<p>Merci d'avoir pris la peine d'avoir fait un exercice, je ne l'avais pas vu cependant c'est une très bonne initiative et très pédagogique.
<br/>
Pour la peine, je vous donne un point de réputation aux deux protagonistes de la discussion <span style="font-size:8px;">(et aussi parce qu'il y a Mint dans la signature)</span>.</p>
<p>Good job !</p> -
<pre class="ipsCode prettyprint">
#include <stdio.h>
#include <string.h>int my_strend(char *s, char *t);
int my_strlen(char *str);int main()
{
int *result;result[0] = my_strend("TEST", "ST"); result[1] = my_strend("Melinyel", "Forum"); printf("%d\n", result[0]); printf("%d\n", result[1]); return (0);}
int my_strend(char *s, char *t)
{
int lenght_s;
int lenght_t;
int i;
int j;lenght_s = strlen(s); lenght_t = strlen(t); j = 0; // On regarde si les dernières lettres de "s" correspondent à "t". for (i = lenght_s - lenght_t; i < lenght_s; i++) { if (s[i] != t[j]) return (0); j++; } return (1);}
int my_strlen(char *str)
{
int lenght;for (lenght = 0; str != "\0"; lenght++); return lenght;}
</pre>
<p>Voila mon code mais j'ai un problème avec ma fonction "my_strlen", il faut que je regarde ça de plus près :unsure: </p>
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login