Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (anonymous):

please help I want a program in C or c ++ to find to given string are anagram ..

OpenStudy (anonymous):

Can you be more specific? You are given two strings and must determine if they are anagrams?

OpenStudy (anonymous):

Is there any limit on the character set that is used in the strings, or the time/space complexity of your solution?

OpenStudy (anonymous):

@elementalmagicks :no There is no limit on the charter set.. and im stuck with program so no time compexity also

OpenStudy (anonymous):

So can I assume the problem is of the form char* foo(char* a, char*b){}?

OpenStudy (anonymous):

@elementalmagicks : yes

OpenStudy (anonymous):

Ok, well if you don't care about time complexity, you can always do this (syntax not quite right): for(i = 0; i < a.length; ++i) { get number of times a[i] appears in a. check to see if it appears the same number of times in b. }

OpenStudy (anonymous):

So: for(int i = 0; i < a.length; ++i) { int aCount = 0; for(int j = 0; j < a.length; ++j) { if(a[j] == a[i]) ++aCount; } int bCount = 0; for(int j = 0; j < a.length; ++j) { if(b[j] == a[i]) ++bCount; if(aCount != bCount) return false; } return true; }

OpenStudy (anonymous):

That's probably not the best algorithm, but it's straightforward and not too bad.

OpenStudy (anonymous):

oh sorry, in the third for loop iot should be b.length, not a.length

OpenStudy (anonymous):

Although you could also check to make sure they are the same length at the start to avoid out of bounds errors. The code currently doesn't check for that!

OpenStudy (anonymous):

how it is working let input is debitcard and badcredit

OpenStudy (anonymous):

huh??

OpenStudy (anonymous):

@elementalmagicks : thanks for the help buddy but i can't get the algo of your program :( :(

OpenStudy (e.mccormick):

If you find the length of the string, you can iterate for half that. x=len(str) for i = 1 to x/2 if str[i]=str[x-i] for all i, then it is an anagram

OpenStudy (anonymous):

No, E.Mccormick is wrong. He is checking if the string is a palindrome. He also isn't 0 indexing.

OpenStudy (e.mccormick):

Quite right! I confused anagram and palindrome mentally. The index of 0 was a minor mistake, thinking letter and not code. But it was just some psudocode. The palindrome mistake is much bigger! For an anagram you are going to need some sort of permutations, which is not at all fast. Sort of like the traveling salesman problem. There is no easy solution.

OpenStudy (e.mccormick):

I would first eliminate any words or phrases that do not have the same number of letters. They won't qualify at all. One way to cut it down more is to add together the integer value of all the character. Change everything to either upper or lower case, then add them. So Four and Fore would become FOUR and FORE. Those become 70+79+85+82=316 and 70+79+82+69=300, because 316\(ne\)300, there is no need to test any deeper. At times, two words of the same length could have the same values, so you still need to do extensive testing after, but it eliminates a lot of possibilities.

OpenStudy (anonymous):

@e.mccormick : But this approch may not work as combination of diffrent integer value can give same result a+b+e+f=a+b+j+a

OpenStudy (e.mccormick):

Like I said, it is a way to cut down on if you need to do an extended test. It is not a solution. It is a limit so that your solution runs faster. Anything that fails the easy limit is discarded. Anything that passes needs a permutations comparison.

OpenStudy (e.mccormick):

Hmmm... or anything that passes you do a sort on and see if it matches. That might be a lot faster. In fact, a sorted match might be fast by itself. So both phrases as c strings. Strip all spaces. Sort. Compare.

OpenStudy (anonymous):

@e.mccormick yuup.... thanks for the help

OpenStudy (e.mccormick):

If you want to do a character math test like I detailed, do add up the values at the same time you remove spaces. Could also get the length at that point. If the math or length fail, no need to sort.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!