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); }
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); }
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("%s",found); /* Isn't found an int. Change to %d */
Last edited by Aia; 11-14-2007 at 04:51 PM.
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}
It should work if you change the printf() format specifier to %d (assuming found is an int).
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( "%s repeats %d times\n", aqq[i], results[i] ); } getchar(); return 0; }
Last edited by Aia; 11-14-2007 at 05:26 PM.
wow!! sweet!! thanks!!
Would you please explain me the code?
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]; } }
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
equals to 0? why?Code:strcmp(arr[j], aqq[i]) == 0
and what doesdo?Code:results[sizeof aqq / sizeof aqq[0]] = { 0 };
thanks so much.
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".
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.and what doesdo?Code:results[sizeof aqq / sizeof aqq[0]] = { 0 };
thanks so much.
"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
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!
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.