Thread: How to Compare elements in arrays

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    33

    How to Compare elements in arrays

    Code:
     I need some help in  finding elements in array "aqq" that matchs with those in "arr"
    My code just prints found as zero 
    
       for(i=0;i<count;i++)
       {
         found=0;
         for(j=0;j<k;j++)
         {
           if(strcmp(arr[j],aqq[i])==0)
           {
             found++;
           }
         }
         printf("%s",found);
       }

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    strcmp works only with strings and not individual elements of a string.
    Assuming that arr[j]and aqq[i] are 2D arrays and not elements of a string.
    Take the found=0; outside of the first for loop.
    printf("&#37;s",found); /* Isn't found an int. Change to %d */
    Last edited by Aia; 11-14-2007 at 04:51 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    Code:
    But i need to reset found to =0 but i am comparing using more that one string
    
    say aqq has {and, go, to, the, nowhere}
          arr has {it, to, and, and, and, on, to, the, and, go, go, go}

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It should work if you change the printf() format specifier to &#37;d (assuming found is an int).

  5. #5
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by axe View Post
    Code:
    But i need to reset found to =0 but i am comparing using more that one string
    
    say aqq has {and, go, to, the, nowhere}
          arr has {it, to, and, and, and, on, to, the, and, go, go, go}
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
        char aqq[][8]= { "and", "go", "to", "the", "nowhere"};
        char arr[][4] = {"it", "to", "and", "and", "and", "on", "to", "the", "and", "go", "go", "go"};
        int i, j;
        int first_array = sizeof aqq / sizeof aqq[0];
        int second_array = sizeof arr / sizeof arr[0];
        int results[sizeof aqq / sizeof aqq[0]] = { 0 };
        
        for ( i = 0; i < first_array; i++)
        {
         	for ( j = 0; j < second_array; j++ )
         	{
           		if ( strcmp(arr[j], aqq[i]) == 0 )
           		{
             		++results[i];
           		}
        	}
        	printf( "&#37;s repeats %d times\n", aqq[i], results[i] );
        }
        getchar();    
        return 0;
    }
    Last edited by Aia; 11-14-2007 at 05:26 PM.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    33
    wow!! sweet!! thanks!!

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Would you please explain me the code?

  8. #8
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by eXeCuTeR View Post
    Would you please explain me the code?
    What part do you not understand?

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Basically, the more complicated part:
    Code:
        int first_array = sizeof aqq / sizeof aqq[0];
        int second_array = sizeof arr / sizeof arr[0];
        int results[sizeof aqq / sizeof aqq[0]] = { 0 };
        
        for ( i = 0; i < first_array; i++)
        {
         	for ( j = 0; j < second_array; j++ )
         	{
           		if ( strcmp(arr[j], aqq[i]) == 0 )
           		{
             		++results[i];
           		}
        	}

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    sizeof aqq / sizeof aqq[0] - calculates number of elements inthe array

    for ( i = 0; i < first_array; i++) - loops through all elements in the array

    strcmp(arr[j], aqq[i]) - compares current string in one array to string in another array

    ++results[i] - if strings are equall - increases the counter

    So what part exactly is hard to understand?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Code:
    strcmp(arr[j], aqq[i]) == 0
    equals to 0? why?

    and what does
    Code:
    results[sizeof aqq / sizeof aqq[0]] = { 0 };
    do?

    thanks so much.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by eXeCuTeR View Post
    Code:
    strcmp(arr[j], aqq[i]) == 0
    equals to 0? why?
    It's not saying it equals 0, it is part of an "if" statement and is saying "if it is equal to 0", not "it is equal to 0". The strcmp function returns 0 if the two strings are equal so the whole "if" statement effectively says "if the two strings are equal to each other".

    and what does
    Code:
    results[sizeof aqq / sizeof aqq[0]] = { 0 };
    do?

    thanks so much.
    It was already explained what sizeof aqq / sizeof aqq[0] does. So, combining it with the declaration of the integer array we can say that it creates an integer array of 8 elements (the same number of character pointer elements as aqq contains) called results. It also initializes all the values in that array to 0.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Yeah, I meant that why if it's TRUE it will equal to 0.

    Thought it would return 0 if 2 strings are equal at first, guess I was right.

    Thanks mate!

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The reason strcmp() returns 0 for "equal" is that it essentially returns "the a signed difference" - this means that strcmp() can ALSO tell you which if the strings is the greater if they are different. If the return value is > 0, the first string is "greater". If the return value is < 0, the second string is the greater.

    So, strcmp("abcd", "abcc") returns something greater than zero, swap the strings around, and you get a negative return. Note that it's not guaranteed what the VALUE itself is, it may be 1 and -1 for all strings, or it may be (commonly is) the difference between the first different letters in the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  2. Table elements compare...
    By ihtus in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2006, 09:30 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 11:40 AM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM