If what you need is to check if two strings are anagrams by sorting them, you could place the strings in single dimensional character arrays like
char str1[]="silent";
char str2[]="listen";
qsort(str1, strlen(str1), sizeof(str1[0]), cmp);
qsort(str2, strlen(str2), sizeof(str2[0]), cmp);
where cmp()
is a function
int cmp(const void *a, const void *b)
{
return *(const char *)a - *(const char *)b;
}
After the qsort()
calls, use strcmp()
like
if(strcmp(str1, str2)==0)
{
//anagrams
}
Read about qsort()
here and here.
Note that in
const char *str1[] = {"listen"};
the string cannot be modified and likewise in the case of
char *str1[]={"listen"};
only in this case, you might get a run-time error as it is a string literal. See this post.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…