Thread: Need help with sorting strings alphabetically

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    Need help with sorting strings alphabetically

    Hi,

    I "want" to sort 10 strings alphabetically using a function (I am always honest, even in forums, so this is an exercise that I must to solve).

    I tried to solve this exercise but I get several errors in several lines, almost the same in every of them:

    error: invalid conversion from 'char' to 'const char*' [-fpermissive]

    This is the code that I have. The function is sort_str and I am almost sure that the nested loop for, for is ok since is bubble method, checked in the following website: C program for bubble sort | Programming Simplified

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void sort_str(char *strings) {
        char *temp;
    
    
        for(int i=0;i<10;i++) {
            for (int j=i+1; j<10; j++) {
                if (strcmp(strings[i],strings[j])>0) {
                    strcpy(temp,strings[i]);
                    strcpy(strings[i],strings[j]);
                    strcpy(strings[j],temp);
                }
            }
        }
    }
    int main()
    {
    
    
        char *strings;
    
    
        printf("Enter all the strings: \n");
        for(int i=0;i<10;i++) {
            gets(strings[i]);
        }
        sort_str(strings);
        printf("Strings sorted alphabetically: ");
        for(int i=0;i<10;i++) {
            puts(strings[i]);
        }
        return 0;
    }
    Could you help me to fix the errors?

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should be getting many more errors or warnings than that one you have reported:
    main.c||In function ‘sort_str’:|
    main.c|12|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/string.h|144|note: expected ‘const char *’ but argument is of type ‘char’|
    main.c|12|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/string.h|144|note: expected ‘const char *’ but argument is of type ‘char’|
    main.c|13|error: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/string.h|129|note: expected ‘const char * restrict’ but argument is of type ‘char’|
    main.c|14|error: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/string.h|129|note: expected ‘char * restrict’ but argument is of type ‘char’|
    main.c|14|error: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/string.h|129|note: expected ‘const char * restrict’ but argument is of type ‘char’|
    main.c|15|error: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/string.h|129|note: expected ‘char * restrict’ but argument is of type ‘char’|
    main.c||In function ‘main’:|
    main.c|29|error: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]|
    main.c|34|error: passing argument 1 of ‘puts’ makes pointer from integer without a cast [-Wint-conversion]|
    /usr/include/stdio.h|695|note: expected ‘const char *’ but argument is of type ‘char’|
    For the first several errors you're using strcmp to compare two characters instead of comparing two strings (you don't need the braces). It looks like you need to study up on how to use C-strings.

    The next error:
    implicit declaration of function ‘gets’
    You should never, Never, NEVER use gets() this dangerous function can never be used safely, which is why it has been removed from the current C standard, hence the reason my compiler issues an error message.

    For the last error puts() expects a string argument, you're passing a single character to this function instead of the string, again you need to study up on how to use C-strings and learn the difference between a single character and a C-string.


    Jim

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    You should be getting many more errors or warnings than that one you have reported:


    For the first several errors you're using strcmp to compare two characters instead of comparing two strings (you don't need the braces). It looks like you need to study up on how to use C-strings.

    The next error: You should never, Never, NEVER use gets() this dangerous function can never be used safely, which is why it has been removed from the current C standard, hence the reason my compiler issues an error message.

    For the last error puts() expects a string argument, you're passing a single character to this function instead of the string, again you need to study up on how to use C-strings and learn the difference between a single character and a C-string.


    Jim
    Thank you for your reply Jim, is there any good resource to study how to compare strings?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Thank you for your reply Jim, is there any good resource to study how to compare strings?
    Your problem is not with comparing strings, your problem is because you don't seem to know the difference between strings and single characters.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        // create an array of char.
        char my_string[100] = "This is a test";
    
        // Print the first character of the string.
        printf("%c\n", my_string[0]);
    
        // Now print the string.
        printf("%s\n", my_string);
    
    
        return 0;
    }
    Do you notice the difference?

    Jim

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Your problem is not with comparing strings, your problem is because you don't seem to know the difference between strings and single characters.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        // create an array of char.
        char my_string[100] = "This is a test";
    
        // Print the first character of the string.
        printf("%c\n", my_string[0]);
    
        // Now print the string.
        printf("%s\n", my_string);
    
    
        return 0;
    }
    Do you notice the difference?

    Jim
    Yes, the first one prints a character, the second a string. I know that because I learned the last week that %c is for printing characters and %s to print strings.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I know that because I learned the last week that %c is for printing characters and %s to print strings.
    Did you notice the other bigger difference?

    Look how the variable is used to print the character versus printing the string.

    Jim

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    101
    Some modifications I made but the program crash:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void sort_str(char *strings[10]) {
        char *temp;
    
    
        for(int i=1;i<10;i++) {
            for (int j=1; j<10; j++) {
                if (strcmp(strings[j],strings[j+1])>0) {
                    strcpy(temp,strings[j]);
                    strcpy(strings[j],strings[j+1]);
                    strcpy(strings[j+1],temp);
                }
            }
        }
    }
    int main()
    {
    
    
        char *strings[10];
    
    
        printf("Enter the strings: \n");
        for(int i=0;i<10;i++) {
            scanf("%s", &strings[i]);
        }
        sort_str(strings);
        printf("Strings sorted alphabetically: ");
        for(int i=0;i<10;i++) {
            printf("%s",strings[i]);
        }
        return 0;
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Some modifications I made but the program crash:
    Why did you create a pointer to you array of char? Where do you assign any memory for that pointer.

    You didn't answer my last question and it seems that you're still missing the major point of my demo program.

    Do you know the difference between my_string[0] and my_string in those calls to printf()?

    Jim

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Why did you create a pointer to you array of char? Where do you assign any memory for that pointer.

    You didn't answer my last question and it seems that you're still missing the major point of my demo program.

    Do you know the difference between my_string[0] and my_string in those calls to printf()?

    Jim
    my_string[0] means the first position of the array, my_string is the whole array. This is at least what I understand.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This is at least what I understand.
    Very good. Now you do understand that when talking about strings that every element of the array will hold single characters. Ie the string "Hello" is actually made up of the individual characters 'H', 'e', 'l', 'l', 'o', '\n'? That last character '\n' is the end of string characters, and every string must contain this special character. When you want to refer to the individual characters you need to select th e proper character ie. string[1] refers to the 'e' character.

    Now when you try to pass a string to a function:

    Code:
    void pass_string(char my_string[]);
    void pass_character( char my_character);
    You need to pass the address of the string. You do this by using the name of the string without any brackets:

    Code:
       char my_string[] = "Hello";
       pass_string(my_string);         // Pass the whole string into the function.
       pass_character(my_string[1]); // Pass the single character 'e' into the function.

    Jim

  11. #11
    Registered User
    Join Date
    May 2017
    Posts
    101
    I am reading the strings by user input through keyboard, I should not use charmy_string[] = "Hello"

    I realized that in the last code the scanf and printf are incorrect. I removed & and [i] but still, the program crash. I get 2 warnings:

    |29|warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=]|
    |34|warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=]|

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your modified code.

    It looks like you're still using a pointer to your array.

    Jim

  13. #13
    Registered User
    Join Date
    May 2017
    Posts
    101
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void sort_srt(char *strings[10]) {
        char *temp;
    
    
        for(int i=0;i<10;i++) {
            for (int j=0; j<10; j++) {
                if (strcmp(strings[j],strings[j+1])>0) {
                    strcpy(temp,strings[j]);
                    strcpy(strings[j],strings[j+1]);
                    strcpy(strings[j+1],temp);
                }
            }
        }
    }
    int main()
    {
    
    
        char *strings[10];
    
    
        printf("Insert 10 strings: \n");
        for(int i=0;i<10;i++) {
            scanf("%s",strings);
        }
        sort_srt(strings);
        printf("Strings sorted alphabetically: ");
        for(int i=0;i<10;i++) {
            printf("%s",strings);
        }
        return 0;
    }

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at this snippet:
    Code:
    int main()
    {
        char *strings[10];
    What is the point of that pointer? Where did you allocate memory for that pointer?

    If you want an array of strings then you need to allocate memory for the strings. Probably something more like:

    Code:
       char strings[10[100];  //Create an array of strings with each string having room for 100 characters.
    Then in your scanf() function you will need to tell the scanf() function what array element you want to access. Something like:

    Code:
        printf("Insert 10 strings: \n");
        for(int i=0;i<10;i++) {
            scanf("%s",strings[i]); // Access element i of the array of strings.
        }
    Also your sort function will need modifying. Something more like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void sort_srt(char strings[10][100]) {
        char temp[100]; // You must allocate memory for this variable.
    
    
        for(int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (strcmp(strings[j], strings[j+1]) > 0) {
                    strcpy(temp, strings[j]);
                    strcpy(strings[j], strings[j+1]);
                    strcpy(strings[j+1], temp);
                }
            }
        }
    }
    
    int main(){
        char strings[10][100];
    
        printf("Insert 10 strings: \n");
        for(int i = 0; i < 10; i++) {
            scanf("%99s",strings[i]); // Don't forget to limit the number of characters scanf() will retrieve to avoid buffer overflows.
        }
        
        sort_srt(strings); 
        
        printf("Strings sorted alphabetically: ");
        for(int i = 0; i < 10; i++) {
            printf("%s",strings[i]);
        }
        
        return 0;
    }
    Jim

  15. #15
    Registered User
    Join Date
    May 2017
    Posts
    101
    Thank you Jim, now works perfectly. However, I feel I need to understand better the strings. the first [] gives the number of chars (elements) in the string (array)? and the second one, in this case [100] is to allocate each char in the memory? So you allowed 100 to save 100 chars as maximum?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting 3 arrays alphabetically
    By Nephilim in forum C Programming
    Replies: 31
    Last Post: 03-28-2011, 09:23 AM
  2. Sorting an array alphabetically?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-09-2011, 04:53 AM
  3. Alphabetically sorting argv[]
    By serg_yegi in forum C Programming
    Replies: 7
    Last Post: 03-27-2010, 07:25 PM
  4. sorting alphabetically
    By ahming in forum C Programming
    Replies: 8
    Last Post: 05-26-2005, 10:34 AM
  5. Sorting Alphabetically
    By orgy230 in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 02:28 PM

Tags for this Thread