alright, well the code I have is a complete mess and there HAS to be a better way of doing it as well.... it's right here

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


int toExit(int exit){
	while(exit != 1 || exit !=2){

		printf("\n\nPlease Choose:\n1) Exit\n2) Run program again\nYour choice: ");
		scanf("%d", &exit);
		printf("\n");

		switch(exit){
		case 1:
			return exit;
		case 2:
			return exit;
		default:
			printf("That is not one of the given options.\n\n");
		}
	}
	
}

int compare(const void * a, const void * b){

	//???
        return 0;
}

int main(void) {

	int exit=0, i;
	int lengthX, lengthA;
	char *Xsequence, *Asequence;
	
	while(exit != 1){
  
	printf("Please enter the length of X: ");
	scanf("%d", &lengthX);

	Xsequence = (char*) malloc(lengthX*sizeof(char));


	for(i=0;i<lengthX+1;i++){
		if(i==lengthX) {
			Xsequence[i] = '\0';
		}
		else {
			scanf("%s", &Xsequence[i]);
		}
		printf("Value is %d\n", i);
	}

	qsort (Xsequence, lengthX, sizeof(char), compare);

	printf("Xsequence is %s\n", Xsequence);


	exit = toExit(exit);

	}
  // return 0; 
}

The thing I can't get to work is qsort, mainly because I don't have a way to make 'compare' work properly. I don't EVER code in C so handling strings is completely foreign to me when you create the array through a malloc command. A problem with the way i'm doing it (maybe it is just an inherent c thing?) is that I can't refer to an index of the array without causing a runtime crash.


All this part of the code needs to do is take in a length of the string the user plans on using, then accept each character from the user one at a time (yes it needs to be done this way) then sort the characters into an alphabetical order within the array.

I don't know what else to even search for to solve this so any advice you might have would be highly appreciated.

Update: I've cleaned it up a bit and gave the compare function a shot, but it still doesn't work. Can anyone tell me why?

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


int toExit(int exit){
	while(exit != 1 || exit !=2){

		printf("\n\nPlease Choose:\n1) Exit\n2) Run program again\nYour choice: ");
		scanf("%d", &exit);
		printf("\n");

		switch(exit){
		case 1:
			return exit;
		case 2:
			return exit;
		default:
			printf("That is not one of the given options.\n\n");
		}
	}
	
}

int compare(const void *a, const void *b){

	const char* a1 = *(const char**)a;
	const char* b1 = *(const char**)b;
	return strcmp(a1,b1);
}

int main(void) {

	int exit=0, i, j;
	int lengthX, lengthA;
	char *Xsequence, *Asequence, *_$;
	
	while(exit != 1){
  

	printf("please enter the length of sequence A: ");
	scanf("%d", &lengthA);
	printf("please enter the length of sequence X: ");
	scanf("%d", &lengthX);

	printf("\n");  //spacing, visual look of the program

	Asequence =(char*) malloc(lengthA*sizeof(char));
	Xsequence =(char*) malloc(lengthX*sizeof(char));

	for(j=0;j<=lengthA-1;j++)
	{
		printf("Element %d of A: ",j+1);
		scanf("%s", &Asequence[j]);
	}

	printf("Last Element of A (looking for \"$\"): ");
	scanf("%s", &_$);
	printf("\n");  //spacing, visual look of the program

	for(j=0;j<=lengthX-1;j++)
	{
		printf("Element %d of X: ",j+1);
		scanf("%s", &Xsequence[j]);
	}

	printf("Last Element of X (looking for \"$\"): ");
	scanf("%s", &_$);
	printf("\n");  //spacing, visual look of the program

	qsort (Xsequence, lengthX, sizeof(char*), compare);
	printf("The \"A\" sequence is: %s\n",Asequence);
	printf("The \"X\" sequence is: %s\n",Xsequence);


	exit = toExit(exit);

	}
  // return 0; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.