I made a function to find out if two words are anagrams... But I didn't want to modify the two original strings. And my function modifies one of the strings, the string that I lower in an char
auxiliary * ( sAux
) (I did it precisely so as not to modify the original string). In my opinion, the auxiliary pointer is pointing to the same place as the pointer that arrives as a parameter to the function (the one of the string that I do not want to modify). But I can't figure out how to fix it.
I show you the function. The string that is modified is the call s1
( const char*s1
-parameter that the function receives). Note that I must not modify the original strings and must use pointer arithmetic.
int esAnagrama(const char *s1, const char *s2)//s1 se modifica
{
char*sAux=(char*)s1,*s2Aux=sAux;//puedo observar que apuntan a lo mismo- a la misma direccion de memoria.
int i=0,j=0,k=0;
while(*(sAux+i)&&*(s2+k))
{
if((*(sAux+i)!=*(s2+k))&&*(s2+k)!=' '&&*(sAux+i)!=*(s2+k)+32&&*(sAux+i)!=*(s2+k)-32)
{
if(*(sAux+i)!=' ')
{
*(s2Aux+j)=*(sAux+i);///si no encontro la letra la dejo en s2aux
j++;
}
}
else k++;//si encontro la letra sigo recorriendo la cadena
i++;
if(*(sAux+i)=='\0'&&i!=j)
{
*(s2Aux+j)='\0';
sAux=s2Aux;
i=j=0;
}
}
return(!*(s2+k)&&!*(sAux+i));
}
Any suggestions on how to fix it so that the s1
original string is not modified? I tried to allocate memory for the char*
one where I lowered the original string but it gave the result I expected.