-1
static int myCompare (const void * a, const void * b)
{
    return strcmp (*(const char **) a, *(const char **) b);
}

void sort1(const char *str1[],int n1)
{
    qsort (str1,n1,sizeof (const char *), myCompare);

}
void sort2(const char *str2[], int n2)
{
    qsort( str2, n2, sizeof (const char *),myCompare);
}

int main ()
{
    const char *str1[] = {"listen"};
    const char *str2[] = {"silent"};

    int n1 = sizeof(str1)/sizeof(str1[0]);
    int n2 = sizeof(str2)/sizeof(str2[0]);

    sort1(str1,n1);
    sort2(str2,n2);

    int x = strcmp(*str1,*str2);

    if(x==0) 
     printf("\n Both The Strings Are Anagram\n");

    else
    printf("\n Strings Are Not Anagram \n"); 
    return 0;
}

I wish to sort the strings and then compare them, to check if they are Anagram.

The Problem is Strings do not get sorted.

Papershine
  • 4,995
  • 2
  • 24
  • 48
  • Its a good time to [learn how to debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Show us what you have attempted to debug it. If you don't know how to use debuggers, learn it. As a starting point, why is str1 and str2 an array of char pointers? –  Jul 15 '18 at 07:27
  • 1
    Well, you have sorted arrays of arrays of chars with one element. As there is only one element, nothing changed. Did you meant to sort characters in those strings, not arrays of characters in an array? – KamilCuk Jul 15 '18 at 07:35
  • But sorting is not a way of checking if strings are anagrams, just if they consist of the same characters. To check if they are anagrams, compare lengths and iterate one string backwards other forwards and see if they're the same. – KamilCuk Jul 15 '18 at 07:36
  • I think what you want to do is to sort the characters in a string and check if other strings have same pattern. E.g. if strings are `abcda` and `bacda` then after sorting characters the strings will be `aabcd` and `aabcd` which are same indicating they are anagrams. – kiner_shah Jul 15 '18 at 07:40
  • @ShubhamSharma, why there are two sort functions: `sort1()` and `sort2()`? I think one sort function is enough! Also why `(const char **)` in compare function? Wouldn't `(const char*)` work? – kiner_shah Jul 15 '18 at 07:44
  • 4
    @KamilCuk No, you are thinking of palindromes. – Blastfurnace Jul 15 '18 at 07:48
  • 1
    @kiner_shah - Yes exactly this is what i'm trying to do. – Shubham Sharma Jul 15 '18 at 09:23
  • @KamilCuk sir, To check if strings are Anagrams we can sort the strings and then compare them easily, this can also be one of the way. The mentioned procedure in your comment will also work. Thanks. – Shubham Sharma Jul 15 '18 at 09:26
  • Did you write this code yourself? – alk Jul 15 '18 at 10:23

1 Answers1

3

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.

J...S
  • 5,079
  • 1
  • 20
  • 35