tags:

views:

107

answers:

7
A: 

I think you meant strcpy (or maybe better, strncpy) on these lines.

            strcmpi(temp,string2d[out]);
            strcmpi(string2d[out],string2d[in]);
            strcmpi(string2d[in],temp);
Roddy
A: 

Where you have

                strcmpi(temp,string2d[out]);
                strcmpi(string2d[out],string2d[in]);
                strcmpi(string2d[in],temp);

you probably meant strcpy

Brian Hooper
+5  A: 

Actually, the problem seems to be on the line

for(in=out+1;out<5;in++)

You increment in, but check if out is smaller than 5.

xyzzy
+1  A: 

I suspect the infinite loop:

for(in=out+1;out<5;in++)
{
    x=strcmpi(string2d[out],string2d[in]);
    if(x>1)
    {
        strcmpi(temp,string2d[out]);
        strcmpi(string2d[out],string2d[in]);
        strcmpi(string2d[in],temp);
    }
}

Your loop condition out < 5 is never changed, I suspect you meant in < 5.

Also as previously mentioned, you are probably using strcmpi instead of strcpy. In addition strcmp* return an integer either lesser than, equal to, or greater than 0, your code compares this against 1

Hasturkun
A: 

A few notes:

When using the function strcmpi, you should check to see if the return value is less than, equal to, or greater than zero, not 1.

The inner-most part of your code should probably be using strncpy instead of strcmpi. The current code does not actually do anything.

What do you mean your program "halts"? How far does it get? Try placing some debug printf statements in key places (such as after the input loop and at the beginning of each iteration of the for loops) to get a better idea of what part of your code is mis-behaving.

bta
A: 

As already said, your three strcmpi should be a copy. Note also that if the first strcmpi (the right one) is as strcmp, your x>1 is always false and the if is never executed.

ShinTakezou
A: 

One method using qsort (tested) :

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

//Changeable constants
const size_t MAX_LENGTH = 100;
const size_t N_NAMES = 10;

//Simple alias for lazy ones
typedef char String[MAX_LENGTH];

//fgets keeps the \n at the end of the
//returned string : this function removes it
void remove_end_rc(char * const string) {
    size_t const len = strlen(string);
    if(len && string[len-1] == '\n')
        string[len-1] = '\0';
}

//Input function
void ask_names(String names[N_NAMES]) {
    for(size_t i = 0 ; i < N_NAMES ; ++i) {
        printf("Name %u ? ", i+1);

        fgets(names[i], MAX_LENGTH, stdin);
        remove_end_rc(names[i]);
    }
}

//Output function
void print_names(String const names[N_NAMES]) {
    printf("Sorted :\n");

    for(size_t i = 0 ; i < N_NAMES ; ++i) {
        printf("%u) %s\n", i+1, names[i]);
    }
}

int alpha_cmp(void const *str1, void const *str2 ) {
    return strcmp((char const*)str1,(char const*)str2);
}

int main(void) {

    String names[N_NAMES] = {""};

    ask_names(names);
    //Sort alphabetically using strcmp
    qsort(names, N_NAMES, MAX_LENGTH, alpha_cmp);
    print_names(names);

    return 0;
}

Another method without qsort(), using bubble algorithm :

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

/** Types *************************************/

//Changeable constants
#define MAX_LENGTH 100
#define N_NAMES 10

//Simple aliases for lazy ones
typedef char String[MAX_LENGTH];
typedef String Names[N_NAMES];

/** Input/Output ******************************/

//fgets keeps the \n at the end of the
//returned string : this function removes it
void remove_end_rc(char * const string) {
    size_t const len = strlen(string);
    if(len && string[len-1] == '\n')
        string[len-1] = '\0';
}

//Input function
void ask_names(Names names) {
    for(size_t i = 0 ; i < N_NAMES ; ++i) {
        printf("Name %u ? ", i+1);

        fgets(names[i], MAX_LENGTH, stdin);
        remove_end_rc(names[i]);
    }
}

//Output function
void print_names(Names names) {
    printf("Sorted :\n");

    for(size_t i = 0 ; i < N_NAMES ; ++i) {
        printf("%u) %s\n", i+1, names[i]);
    }
}

/** Sorting *************************************/

//Explicit
void swap_str(String s1, String s2) {
    String temp = "";

    strcpy(temp, s1);
    strcpy(s1, s2);
    strcpy(s2, temp);
}

#include <stdbool.h>

//Sorts alphabetically using bubble algorithm
void alpha_sort(Names names)
{
    bool swapped;

    do {
        swapped = false;

        for(size_t i = 0 ; i < N_NAMES-1 ; ++i) {
            if(strcmp(names[i], names[i+1])) {
                swap_str(names[i], names[i+1]);
                swapped = true;
            }
        }
    }while(!swapped);
}

/** Main program **********************************/

int main(void) {

    Names names = {""};

    ask_names(names);
    alpha_sort(names);
    print_names(names);

    return 0;
}

You could ameliorate it by taking care of cases (lower, upper), symbols... But basically it does its job.

Mister Mystère
I dont want to use qsort as i am not taught pointers yet
fahad
It's not about pointers. Or, at first sight. Behind, there is a pointer to function but implicit cast makes it invisible to developper, just like an array is implicitly castable into a pointer to the first element. That's right, you used pointers without knowing it. However, you could avoid qsort if you will by implementing whatever sorting technique you like (bubble etc.) with strcmp() comparison.
Mister Mystère
I edited my post above with a method without qsort() (tested as well). This should do the trick, and it's commented so that you understand. If you don't understand how alpha_sort() works, Wiki bubble sort.
Mister Mystère