Monday, July 27, 2009

C Programmers, help me please...?

how can I count letters that are similar in two given words?.


example:





flower


water





both words have w, e %26amp; r. 'flower' has 3 letters similar to 'water'. 'water' has also 3 letters similar to 'flower'. the number of similar letters sums to 6.





what functions should I use? and how?

C Programmers, help me please...?
sort(string1,sizeof(char),strlen(string1...


sort(string2,sizeof(char),strlen(strin...


ptr=string1;


while(*(prt++))


{


if (bsearch(*ptr,string2,sizeof(char),strle...


no_of_common_chars++;


}


Its of order O(nlogm)
Reply:It's kind of silly to count it both ways: the words flower and water have 3 letters in common and they are always the same ones.





Suggested solution: for each of the characters in either one of the strings, check if it also occurs in the other string. Something like this:





int count = 0;


for(int i = 0; i %26lt; strlen(str1); i++)


{


     if(strpos(str2, str1[i]) %26gt; -1) count++;


}





If you really want, then you can multiply count by 2 to get the number 6 in stead of 3. But, again, it seems silly to me.
Reply:but answer is 3 dear


No comments:

Post a Comment