-4

my program does not compile. It keeps on saying: [ERROR] invalid conversion from 'char' to 'char*' It should be a program that identifies if two strings are an anagram of each other. I used a sorting method but i don't know if it will work. Hope you guys could help me out. P.S I can only use strings and arrays.

int main ()
{
    char sString_1[100], sString2[100], store[50];
    int  j, i;

    printf("Enter String 1: ");
    gets(sString_1);
    printf("\nEnter String 2: ");
    gets(sString2);

    if (strlen(sString_1) != strlen(sString2))
        printf("%s and %s are not anagrams", sString_1, sString2);
    else
    {
        for(i = 0; i < strlen(sString_1); ++i)
        {
            for (j=i+1 ; j <= strlen(sString_1); ++j)
            {
                if (strcmp(sString_1[i], sString2[j]) > 0)
                {
                    strcpy(store,sString_1[i]);
                    strcpy(sString_1[i],sString_1[j]);
                    strcpy(sString_1[j],store);
                }

            }
        }

        for(i = 0; i < strlen(sString2); i++)
        {
            for (j= i + 1; j <= strlen(sString2); j++)
            {
                if (strcmp(sString2[i], sString2[j]) >0)
                {
                    strcpy(store,sString2[i]);
                    strcpy(sString2[i],sString2[j]);
                    strcpy(sString2[j],store);
                }
            }
        }

        if (strcmp(sString_1, sString2) == 0)
            printf("ANAGRAM");
        else
            printf("NOT");
    }
}
sabbahillel
  • 4,357
  • 1
  • 19
  • 36

3 Answers3

0

strcmp(sString_1[i], sString2[j])

The arguments to strcmp shoud be of type const char *, yet you pass single chars.

bipll
  • 11,747
  • 1
  • 18
  • 32
0

In general when you use p[i] then you are changing the pointer (or array) p into what it points to, e.g. a character. It is like writing *(p+i). To get the address of that character you can either write p+i or &p[i].

Example: to fix the errors you have to change

strcmp(sString_1[i], sString2[j])

into

strcmp(&sString_1[i], &sString2[j])
Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
0

Do you want to make like this?

    char sString_1[100], sString2[100], store;            // store is 1char
//=======omit==========
    else
    {
        for(i = 0; i < strlen(sString_1); ++i)
        {
            for (j=i+1 ; j < strlen(sString_1); ++j)         // "<="→"<"
            {
                if (sString_1[i] > sString2[j])              // char
                {
                    store = sString_1[i];                    // char
                    sString_1[i] = sString_1[j];
                    sString_1[j] = store;
                }
            }
        }
        for(i = 0; i < strlen(sString2); i++)
        {
            for (j= i + 1; j < strlen(sString2); j++)         // "<="→"<"
            {
                if (sString2[i] > sString2[j])                // char
                {
                    store = sString2[i];                      // char
                    sString2[i] = sString2[j];
                    sString2[j] = store;
                }
            }
        }
//=======omit==========

1 loop 4250 2450 0452
2 loop 0254
3 loop 0245

nariuji
  • 288
  • 1
  • 6