Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • 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
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. finding longest word

finding longest word

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 Posters 0 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Anonygeeker
    wrote on last edited by
    #1

    This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?

    L J CPalliniC 3 Replies Last reply
    0
    • A Anonygeeker

      This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You only check the length of each word when you encounter a space character. So if the last character is not a space then when the loop ends you still have an unchecked word in array b. Your code would be clearer if you formatted it properly and used <pre> tags round it. And also used meaningful names for your variables rather than single letters.

      A 1 Reply Last reply
      0
      • L Lost User

        You only check the length of each word when you encounter a space character. So if the last character is not a space then when the loop ends you still have an unchecked word in array b. Your code would be clearer if you formatted it properly and used <pre> tags round it. And also used meaningful names for your variables rather than single letters.

        A Offline
        A Offline
        Anonygeeker
        wrote on last edited by
        #3

        what condition I can add here to avoid it?

        L 1 Reply Last reply
        0
        • A Anonygeeker

          This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          It is essential to "see" the code properly indented and formatted. Then you can recognise - besides the problems mentioned by Richard - that there is a missing closing parentheses (which should also issue a compiler error if not present behind the posted code):

          void find_long(char *a)
          {
          int l,i,l1=0,j=0,l2=0;
          char b[50],c[50];
          l=strlen(a);
          for (i = 0; i < l; i++) {
          if (a[i] != ' ') {
          b[j] = a[i];
          printf("%c ",b[j]);
          j++;
          }
          else if (a[i] == ' ') {
          b[j] = '\0';
          if (strlen(b) > l1) {
          strcpy(c,b);
          l1 = strlen(b);
          // Inserted missing parentheses here
          }
          j = 0;
          }
          else {
          printf("\nEOF");}
          }
          }
          printf("\nlongest is \n");
          printf(c);
          // Added missing parentheses here
          }

          1 Reply Last reply
          0
          • A Anonygeeker

            what condition I can add here to avoid it?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            At the end of the loop you just need to check the last word in array b.

            A 1 Reply Last reply
            0
            • L Lost User

              At the end of the loop you just need to check the last word in array b.

              A Offline
              A Offline
              Anonygeeker
              wrote on last edited by
              #6

              It worked Thanks

              1 Reply Last reply
              0
              • A Anonygeeker

                This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Try

                #include enum State
                {
                INSIDE_BLANKS,
                INSIDE_WORD
                };

                void find_longest_word( const char * a, const char ** pps, const char ** ppe);

                int main()
                {
                const char * foo = "alpha beta gamma delta epsilon ";

                const char *ps, *pe;

                find_longest_word( foo, &ps, &pe);

                if ( pe-ps > 0)
                {
                printf("longest word length = %ld\n", (pe-ps));
                while (ps != pe)
                {
                printf("%c", *ps);
                ++ps;
                }
                printf("\n");
                }
                return 0;
                }

                void find_longest_word( const char * a, const char ** pps, const char **ppe)
                {
                const char * ps = a;
                const char * pe = a;
                *pps = *ppe = a;

                enum State state = INSIDE_BLANKS;

                while ( *a != '\0')
                {
                if ( state == INSIDE_BLANKS)
                {
                if ( *a != ' ')
                {
                state = INSIDE_WORD;
                ps = a;
                }
                }
                else // inside word
                {
                if ( *a == ' ')
                {
                pe = a;
                if ( pe - ps > *ppe - *pps)
                {
                *pps = ps;
                *ppe = pe;
                }
                state = INSIDE_BLANKS;
                }
                }
                ++a;
                }
                // special handling of (possible) last word
                if ( state == INSIDE_WORD)
                {
                if ( pe - ps > *pps - *pps)
                {
                *pps = ps;
                *ppe = pe;
                }
                }
                }

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

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