user379888
user379888

Reputation:

arrange strings in alphabetic order

/* write a program to sort names entered in an array in ascending order */ When i enter the names into the array the program halts .Anyone knows why?

#include<stdio.h>
#include<string.h>
void main(void)
{
/*  write a program to sort names entered in an array in ascending order */
    int in,out,i,x;
    char temp[30],string2d[5][30];
    printf("ENTER NAMES IN THE ARRAY:");
    for(i=0;i<5 ;i++)
    {
        gets(string2d[i]);
    }
        for(out=0;out<5-1;out++)
        {
            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);
                }
            }
        }

    for(i=0;i<5;i++)
    {
        puts(string2d[i]);
    }
    getch();
}

i have seen the comments and made changes to the real program but the program still loops between the in loop and the i

Upvotes: 0

Views: 4599

Answers (7)

Mister Myst&#232;re
Mister Myst&#232;re

Reputation: 1002

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.

Upvotes: 0

ShinTakezou
ShinTakezou

Reputation: 9681

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.

Upvotes: 0

bta
bta

Reputation: 45087

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.

Upvotes: 0

Hasturkun
Hasturkun

Reputation: 36422

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

Upvotes: 1

xyzzy
xyzzy

Reputation: 51

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.

Upvotes: 5

Brian Hooper
Brian Hooper

Reputation: 22084

Where you have

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

you probably meant strcpy

Upvotes: 0

Roddy
Roddy

Reputation: 68074

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);

Upvotes: 0

Related Questions