Question: In PROGRAMMIN LANGUGE C -right now this program sorts characters, how would I get it to sort strings alphabetically? EX INPUT:Hello World Alpha OUTPUT: Alpha
In PROGRAMMIN LANGUGE C
-right now this program sorts characters, how would I get it to sort strings alphabetically?
EX
INPUT:"Hello World Alpha"
OUTPUT:
Alpha
Hello
World
#include
#include
#include
#include
struct Node{
void *data;
struct Node *next;
};
void push(struct Node** head_ref, void *new_data, size_t data_size){
// Allocate meory for node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = malloc(data_size);
new_node->next = (*head_ref);
int i =0;
for(i =0; i *(char *)(new_node->data +i) = *(char *)(new_data +i); (*head_ref) = new_node; } } void copyToArray(struct Node *node, char* charArray){ int index =0; while(node != NULL){ charArray[index] = *(char *)node->data; node = node->next; index++; } charArray[index] = '\0'; } void printList(struct Node *node, void(*fptr) (void *)){ while(node !=NULL){ (*fptr)(node->data); node = node->next; } } void printString(void *c){ printf("%c",*(char *)c); } int main(int argc, char*argv[]){ struct Node *start = NULL; int length = strlen(argv[1]); int i =0; int j =0; for(i = length;i>=0;i--){ if(isalpha(argv[1][i])!= 0||isdigit(argv[1][i] == 0)){ push(&start,&argv[1][i],length); } } printList(start,printString); char *charArray = (char *)malloc(sizeof(char)*length); printf(" Printing out the array "); copyToArray(start,charArray); for(i =0; i< strlen(charArray);i++){ printf("%c",charArray[i]); } printf(" Printing out the sorted Array "); for(i =0; i for(j =i;j if(charArray[i]>charArray[j]){ char temp =charArray[i]; charArray[i]=charArray[j]; charArray[j]=temp; } } } for(i =0; i < strlen(charArray);i++){ printf("%c",charArray[i]); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
