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. Searching string not working

Searching string not working

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsquestion
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

    I have a program to search and find a given string as below : #include #include #define MAX 10 int srch(char *a[],char *b) { int i,r=0; for(i=0;a[i];i++){ printf("%c\n",a[i]); if(!strcmp(a[i],b)){ return 0; } else{ return -1; } } } main() { char *a[]={"abc","xyz","lmn","NULL"}; char *b; int l; printf("\nenter string to find\n"); fgets(b,MAX,stdin); printf("%c",*b); l=srch(a,b); printf("\n%d",l); } But I am getting segmentation fault. Why?

    L CPalliniC 2 Replies Last reply
    0
    • A Anonygeeker

      I have a program to search and find a given string as below : #include #include #define MAX 10 int srch(char *a[],char *b) { int i,r=0; for(i=0;a[i];i++){ printf("%c\n",a[i]); if(!strcmp(a[i],b)){ return 0; } else{ return -1; } } } main() { char *a[]={"abc","xyz","lmn","NULL"}; char *b; int l; printf("\nenter string to find\n"); fgets(b,MAX,stdin); printf("%c",*b); l=srch(a,b); printf("\n%d",l); } But I am getting segmentation fault. Why?

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

      Anonygeeker wrote:

      But I am getting segmentation fault. Why?

      Because your original array does not contain a null terminator. You have:

      char *a[]={"abc","xyz","lmn","NULL"};

      but "NULL" is a quoted string, not a NULL value, so your loop will continue until it causes the fault. You also have:

      printf("%c\n",a[i]);

      but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:

      printf("%s\n",a[i]); // use s to print a character aray

      And finally (although I may have missed other mistakes) you have:

      char *b; // what does this point to?
      int l;
      printf("\nenter string to find\n");
      fgets(b,MAX,stdin); // but b does not point to anything
      printf("%c",*b); // again %c means a single character

      It should be:

      char b[MAX];
      int l;
      printf("\nenter string to find\n");
      fgets(b,MAX,stdin);
      printf("%s",b);

      I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.

      J A CPalliniC 3 Replies Last reply
      0
      • L Lost User

        Anonygeeker wrote:

        But I am getting segmentation fault. Why?

        Because your original array does not contain a null terminator. You have:

        char *a[]={"abc","xyz","lmn","NULL"};

        but "NULL" is a quoted string, not a NULL value, so your loop will continue until it causes the fault. You also have:

        printf("%c\n",a[i]);

        but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:

        printf("%s\n",a[i]); // use s to print a character aray

        And finally (although I may have missed other mistakes) you have:

        char *b; // what does this point to?
        int l;
        printf("\nenter string to find\n");
        fgets(b,MAX,stdin); // but b does not point to anything
        printf("%c",*b); // again %c means a single character

        It should be:

        char b[MAX];
        int l;
        printf("\nenter string to find\n");
        fgets(b,MAX,stdin);
        printf("%s",b);

        I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.

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

        Richard MacCutchan wrote:

        although I may have missed other mistakes

        You have missed the logical error here (which should generate a compiler message that not all paths return a value):

        int srch(char *a[],char *b)
        {
        int i,r=0;
        for(i=0;a[i];i++){
        printf("%c\n",a[i]);
        if(!strcmp(a[i],b)){
        return 0;
        }
        else{
        return -1;
        }
        }
        }

        The loop will - if entered - always left after the first iteration. But it is hard to spot whithout indentation.

        L 1 Reply Last reply
        0
        • L Lost User

          Anonygeeker wrote:

          But I am getting segmentation fault. Why?

          Because your original array does not contain a null terminator. You have:

          char *a[]={"abc","xyz","lmn","NULL"};

          but "NULL" is a quoted string, not a NULL value, so your loop will continue until it causes the fault. You also have:

          printf("%c\n",a[i]);

          but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:

          printf("%s\n",a[i]); // use s to print a character aray

          And finally (although I may have missed other mistakes) you have:

          char *b; // what does this point to?
          int l;
          printf("\nenter string to find\n");
          fgets(b,MAX,stdin); // but b does not point to anything
          printf("%c",*b); // again %c means a single character

          It should be:

          char b[MAX];
          int l;
          printf("\nenter string to find\n");
          fgets(b,MAX,stdin);
          printf("%s",b);

          I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.

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

          thankyou

          1 Reply Last reply
          0
          • L Lost User

            Anonygeeker wrote:

            But I am getting segmentation fault. Why?

            Because your original array does not contain a null terminator. You have:

            char *a[]={"abc","xyz","lmn","NULL"};

            but "NULL" is a quoted string, not a NULL value, so your loop will continue until it causes the fault. You also have:

            printf("%c\n",a[i]);

            but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:

            printf("%s\n",a[i]); // use s to print a character aray

            And finally (although I may have missed other mistakes) you have:

            char *b; // what does this point to?
            int l;
            printf("\nenter string to find\n");
            fgets(b,MAX,stdin); // but b does not point to anything
            printf("%c",*b); // again %c means a single character

            It should be:

            char b[MAX];
            int l;
            printf("\nenter string to find\n");
            fgets(b,MAX,stdin);
            printf("%s",b);

            I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.

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

            Try

            #include #include #define MAX 10

            int srch(const char *a[],char *b)
            {
            int i = 0;
            while ( a[i] != NULL)
            {
            if ( ! strcmp( b, a[i]) ) return i;
            ++i;
            }
            return -1;
            }

            int main()
            {
            int index;
            const char *a[]={"abc","xyz","lmn", NULL};
            char b[MAX];
            printf("\nplease, enter string to find\n");

            if ( fgets(b, MAX, stdin) )
            {
            // remove the newline
            for (index = 0; index < MAX; ++index)
            if ( b[index] == '\n')
            {
            b[index] = '\0';
            break;
            }

            printf("string to find '%s'\\n",b);
            index = srch( a, b);
            if ( index != -1 )
              printf("string found at index = %d\\n", index);
            else
              printf("string not found\\n");
            

            }
            return 0;
            }

            Please note, fgets includes the newline in the string, that's the reason for the clumsy code used to remove it.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • A Anonygeeker

              I have a program to search and find a given string as below : #include #include #define MAX 10 int srch(char *a[],char *b) { int i,r=0; for(i=0;a[i];i++){ printf("%c\n",a[i]); if(!strcmp(a[i],b)){ return 0; } else{ return -1; } } } main() { char *a[]={"abc","xyz","lmn","NULL"}; char *b; int l; printf("\nenter string to find\n"); fgets(b,MAX,stdin); printf("%c",*b); l=srch(a,b); printf("\n%d",l); } But I am getting segmentation fault. Why?

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

              Try

              #include #include #define MAX 10

              int srch(const char *a[],char *b)
              {
              int i = 0;
              while ( a[i] != NULL)
              {
              if ( ! strcmp( b, a[i]) ) return i;
              ++i;
              }
              return -1;
              }

              int main()
              {
              int index;
              const char *a[]={"abc","xyz","lmn", NULL};
              char b[MAX];
              printf("\nplease, enter string to find\n");

              if ( fgets(b, MAX, stdin) )
              {
              // remove the newline
              for (index = 0; index < MAX; ++index)
              if ( b[index] == '\n')
              {
              b[index] = '\0';
              break;
              }

              printf("string to find '%s'\\n",b);
              index = srch( a, b);
              if ( index != -1 )
                printf("string found at index = %d\\n", index);
              else
                printf("string not found\\n");
              

              }
              return 0;
              }

              Please note, fgets includes the newline in the string, that's the reason for the clumsy code used to remove it.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • J Jochen Arndt

                Richard MacCutchan wrote:

                although I may have missed other mistakes

                You have missed the logical error here (which should generate a compiler message that not all paths return a value):

                int srch(char *a[],char *b)
                {
                int i,r=0;
                for(i=0;a[i];i++){
                printf("%c\n",a[i]);
                if(!strcmp(a[i],b)){
                return 0;
                }
                else{
                return -1;
                }
                }
                }

                The loop will - if entered - always left after the first iteration. But it is hard to spot whithout indentation.

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

                I got tired of looking.

                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