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.