I've been trying to complete an exercise where the program must take two words and answer whether or not the words are anagrams of each other.
I was attempting to do it with one function called 'checkAnagram' which was supposed to go through each element of the first word and compare it to each element of the second word until a match is found, then repeat the process for each element of the first until the variable 'found' is either 'true' or 'false'. I then realised that this function was too simplistic and wouldn't consider the number of each letter, the number of characters, etc. and therefore wouldn't give me the correct response anyway.
I now have the teacher's solutions for the exercises which I glanced quickly and saw he used 'for' loops to get the various little tasks done like counting the number of characters, and then checking whether each word contains the same characters, so I will try writing the program that way.
However, I am still curious about why my code doesn't give the expected response. When I run the program, it simply gives me 'NO', no matter what words I input. Can anyone point out to me my error?
if you want a pure anagram (that is, rearrange word one to get word two, using every letter of word one only once) then you can just sort word 1 and word 2 and compare that. If it is the same, they are anagrams. Beyond that, where you start asking if word 2 is IN word one but may not use all the letters of one, it takes more logic.
@seeplus, thanks for the idea! @dutch does make a good point re: trying "ab" and "ac", though! Blame it on my Frankencode, lol!
@dutch; The thing that confuses me a bit here is why should it make a difference whether it stops at the end of the actual string or whether it stops at the limit of the array? For example, if you have two words of 4 characters, there would be the same number of 'slots' used in the array, as well as the null terminator and the vacant slots, so how come it doesn't 'read' that? Is that simply the way it is, or is there a more nuanced answer? (Sorry if this question makes little sense, it's been a fairly long day for me...haha).
@jonnin, That's a cool workaround that I hadn't seen before. I'm not sure if it could be used along with the 'tolower' function to abide by the exercise asking for the anagram solver to ignore whether the characters are uppercase, but I'm definitely going to give it a go, too.
I stumbled upon another little issue - when I try to compare two string lengths, I'm getting bool = 0 despite the two strings being the same length...???
I'm getting bool = 0 despite the two strings being the same length...???
You aren't saving the return value of checkLength(). You are displaying an uninitialized sameLength variable in line 14. You should bool sameLength = checkLength(word1, word2);.
then you can sort it and use that idea.
you could also jack the tolower into a custom comparison function for the sort (and the string compare afterwards). either way.
@Furry Guy - I think my brain must have been switching off when I asked, haha! Thanks.
@jonnin - That does look super geeky! :D Thank you. I will play with that once I've finished the current exercise I am on. (Finished the anagram one earlier today and am trying to move through these exercises at a decent pace).
For this next exercise I am currently trying to create a function which replaces 'c' with 'k' - why, in the case of my code, am I getting this output (sorry I didn't translate the names, I figured the program is pretty self-explanatory :) ):
you loop who knows how many times, checking the same character over and over.
this seems like it is not exactly what you wanted, as it is identical to
if(caracter == 'c')
{
caracter = 'k';
}
I think you want normal[i] instead of caracter.
int longitud;
i = 0; //!!!! this i and the i in the for loop are NOT THE SAME i!! the i on line 32 is not used and should be discarded. You now have THREE DIFFERENT int i in this code: an incoming parameter, the local one you don't need, and the loop variable one. longitud = strlen(normal);
for(i=0; i<strlen(normal); i++) //just put the strlen here, unless you know that it is computed every loop and inefficient and you care about the speed.