0

I was assigned the task of making a program to sort string (without the use of pointers, because we haven't been taught about those yet). But I'm stuck and I need a little help. This is what I have so far:

#include <stdio.h>

int main()
{
    printf("Enter text to be sorted alphabetically:\n");
    char a[100][100]; // This is my array of text. 
    // It has a maximum of 100 words, each with 100 characters.
    int i = 0;
    while(scanf("%c", a[i][100]) != EOF)
    {
            i++; // This is where I get the string from the user.
            // I think this is where the problem is.
    }
    int l, x, j, m = 0;
    char k[100]; // This is the swap variable for the bubble sort.
    for(l = 0; l < i; l++)
    {
            for(j = 0; l < i - l; j++)
            {
                    if(a[j][m] > a[j+1][m]) 
                    { 
                            for(x = 0; x < 100; x++)
                            {
                                    k[m] = a[j][m]; // Bubble sort.
                                    a[j][m] = a[j+1][m];
                                    a[j+1][m] = k[m];
                            }
                            m = 0; // m is set back to 0.
                    }
                    if(a[j][m] == a[j+1][m])
                    {
                            m++; // m is supposed to represent the mth letter.
                            // so if the first two letters are equal, it increases m.
                            j--;
                    }
            }
    }
    printf("Sorted text is: /n");
    for(l = 0; l < i; l++)
    {
            for(j = 0; j < 100; j++)
            {
                    printf("%s", a[l][j]); // Print out the final result.
                    // I think I messed up this one too.
            }
            printf("\n");
    }
}

This code refuses to compile. It says:

format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’

Can someone tell me what I'm doing wrong? Thanks.

1
  • 1
    Actually, the word int in the error could be important. Are you sure you have char a[100][100]? Could it be int a[100][100]? Commented Nov 9, 2013 at 18:27

1 Answer 1

1

For strings, the scanf format specifier is %s

scanf ("%s" ...

and you have to provide the address where to store the string

&a[i][0]

and prey nobody inputs more than 99 chars :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That worked beautifully. The code still doesn't work right, but I resolved my error. I can take it from here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.