Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

NodeBB

  1. Home
  2. Programmation
  3. Développement de logiciels
  4. C
  5. Exercices
  6. strend

strend

Scheduled Pinned Locked Moved Exercices
8 Posts 4 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D
    D
    davydavek
    wrote on last edited by
    #1

    <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>

    C# dev
    github.com/DavyWk

    1 Reply Last reply
    1
    • AlexMogA
      AlexMogA
      AlexMog
      Modérateur spécialisé
      wrote on last edited by
      #2

      <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 &amp;&amp; strlen(tmp) == strlen(t));
      

      }
      </pre>

      Multiplayer GameDev @ Unexpected
      

      Mon CV

      1 Reply Last reply
      1
      • D
        D
        davydavek
        wrote on last edited by
        #3

        <p>@Alex:</p>
        <p> </p>
        <p>strstr retourne a la première apparition de t, donc avec ta version :</p>
        <pre class="ipsCode prettyprint">
        strend("ABDAVYBD","BD");
        </pre>
        <p>retourne 0, alors que ça devrait retourner 1.</p>

        C# dev
        github.com/DavyWk

        1 Reply Last reply
        0
        • AlexMogA
          AlexMogA
          AlexMog
          Modérateur spécialisé
          wrote on last edited by
          #4

          <p>Voilà, j'ai edit mon code 😉 </p>

          Multiplayer GameDev @ Unexpected
          

          Mon CV

          1 Reply Last reply
          1
          • D
            D
            davydavek
            wrote on last edited by
            #5

            <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>

            C# dev
            github.com/DavyWk

            1 Reply Last reply
            0
            • AzadA
              AzadA
              Azad
              wrote on last edited by
              #6

              <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>

              Administrateur du forum.
              Contactez-moi par message privé ou par mail.

              1 Reply Last reply
              1
              • SoulalexS
                SoulalexS
                Soulalex
                wrote on last edited by
                #7

                <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 &lt; 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>

                Soulalex, Administrateur de Melinyel+ E-Mail : [email protected]+ GitHub : https://github.com/Soualex

                1 Reply Last reply
                0
                • D
                  D
                  davydavek
                  wrote on last edited by
                  #8

                  <p>Suffit d'un peu de relecture 😉 </p>
                  <p> </p>
                  <p>Ta fonction <em>my_strlen</em> n’incrémente pas le pointeur <em>str</em>, donc si le premier caractère n'est pas '\0' ça fait une boucle infinie.</p>

                  C# dev
                  github.com/DavyWk

                  1 Reply Last reply
                  0

                  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
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Login or register to search.
                  Powered by NodeBB Contributors
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups