1

I have a collection of strings of planet names in c. I have stored them in a character array. I want to sort them out. I know there's a string comparing method in c but I need to implement mine. In my method(string_compare(char *planet1, char *planet2)), i check characters between two planets. If character of planet1 is after that of plannet2, then I return 1, else 0 is returned. I then rearrange the array items accordingly. At the moment, my loop is not exiting. It is running infinetely. I am also having trouble exchanging the array entries. Please assist.

#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>

//method declaration
int string_compare(char *planet1, char *planet2);

//main method
int main () {
    char *planets[9]= {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
    bool sorted=false;

    int i=0, count = 0;
    do{
    int planets_size = (int)sizeof(planets)/sizeof(planets[0]);
    for(i=0; i< planets_size-1;i++){
        char *planet1_pointer = planets[i];
        char *planet2_pointer = planets[i+1];
        int comparison = string_compare(planet1_pointer,planet2_pointer);
        if(comparison>1){
                planets[i] = planet2_pointer;
                planets[i+1] = planet1_pointer;
                sorted = false;
                break;
            } else {
                if(i==planets_size-1){
                    sorted = true;
                }
            }
        }
        count++;
    } while (sorted==false);
    printf("The planets in alphabetical order are ");
    //printf("%s",planets);

    i=0;
    for (i=0; i<9; i++) {
        printf("%s " , planets[i]);
    }
    return 0;
}

int string_compare(char *planet1, char *planet2){
    int planet1_size = strlen(planet1);
    int planet2_size = strlen(planet2);
    int size=0;

    if(planet1_size<planet2_size){
        size = planet1_size;
    } else{
        size = planet2_size;
    }

    int i=0;
    for(i=0; i < size;i++){
        if((int)planet1[i]<(int)planet2[i]){
            return 0;
        } else if((int)planet1[i]>(int)planet2[i]){
            return 1;
        } else {
            continue;
        }
    }

return 0;
}

My code is above. I am a beginner in c.

7
  • 4
    When you use sizeof on a pointer, you only get the size of the pointer and not what it points to. To get the length of the string, either use strlen, or if you must make the code yourself (as an exercise) remember that strings in C are terminated by the character '\0'. Commented Apr 16, 2016 at 16:04
  • if(i==planets_size){ Never be true. Commented Apr 16, 2016 at 16:06
  • for ease of understanding and readability by us humans: 1) separate code blocks (for, if, else, while, do... while, switch, case, default) via a blank line. 2) follow the axiom: *only one statement per line and (at most) one variable declaration per statement. 3) consistently indent the code. indent after every opening brace '{'. unindent before every closing brace '}. do not use tabs for indenting. Suggest 4 spaces for each indent level. Commented Apr 18, 2016 at 15:56
  • the posted code is trying to combine a for() loop with a do... while() loop. that will not work, the result is the for() loop is run to completion. the the while() loop is entered (and if entered, never exits. Commented Apr 18, 2016 at 16:03
  • please explain why you could not use the strcmp() function instead of writing the string_compare() function. Commented Apr 18, 2016 at 16:14

2 Answers 2

1

You looped using for(i=0; i< planets_size-1;i++). Notice the i< planets_size-1, so the condition if(i==planets_size-1) will always be false since i will be always less than planets_size-1 by the definition of your for loop (in other words never equal).

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

Comments

1

There are two issues with the code. One of them, as pointed out by others, is that if(i==planets_size-1) in the main function will never be true because in the for loop you will not be going beyond planets_size-2.

Another issue is that if(comparison>1) will never be true either because the string_compare function returns either 0 or 1.

Comments

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.