In: Computer Science
IN PROGRAMMING LANGUAGE C
-I am trying to alphbetize a string in descending or to
EX
INPUT: B C D A
OUTPUT:
D
C
B
A
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char*argv[])
{
int MAX = 100000;
int i =0;
int k =0;
int j =0;
char array[MAX];
char split[] = "
,.-!?()0123456789";
int n = 0;
char second[MAX];
printf("Please enter in
a String: ");
//String input
fgets(array,MAX,stdin);
char **first
=(char**)malloc(sizeof(char*));
char *token =
strtok(array,split);
while(token!=
NULL)
{
k = strlen(token);
first=(char **)realloc(first,(n+1)*sizeof(char));
first[n] = (char *)malloc((k+1)*sizeof(char));
memcpy(first[n],token,k*sizeof(char));
token = strtok(NULL,split);
n++;
}
second =
sortedArray(first,n);
for(i =0;
i<n;i++)
{
printf("%s",second[i]);
}
return 0;
}
char *sortedArray(char *sortRay[], int size)
{
int i =0;
int j =0;
char temp[300];
for(i =0; i<size;
i++)
{
for(j =i +1;
j<size;j++)
{
if(strcmp(sortRay[i],sortRay[j])>0)
{
strcpy(temp,sortRay[i]);
strcpy(sortRay[i],sortRay[j]);
strcpy(sortRay[j],temp);
}
}
}
return sortRay;
}
#include <stdio.h>
#include <string.h>
int main (void) {
int MAX = 100000;
char string[MAX];
char temp;
int i, j;
printf("Please enter in a String: ");
fgets(string,MAX,stdin);
printf("String before sorting - %s ", string);
int n = strlen(string);
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] < string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
printf("String after sorting - %s ", string);
return 0;
}