Add solution for string sorting.
This commit is contained in:
parent
3ab40ce245
commit
5a3b146df3
@ -12,27 +12,70 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void gibStringsAus(const char *strings[], unsigned int anzahl);
|
|
||||||
|
#define STRING_GROESSE 10
|
||||||
|
|
||||||
|
|
||||||
|
//void gibStringsAus(const char *strings[], unsigned int anzahl);
|
||||||
|
void gibStringsAus(const char strings[][STRING_GROESSE], unsigned int anzahl);
|
||||||
|
int compare(const void *arg1, const void *arg2);
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const char *strings[] = {"789", "Zorro", "Adam", "Werner", "123", "Bernd", "Hubert", "456"};
|
//const char *strings[] = {"789", "Zorro", "Adam", "Werner", "123", "Bernd", "Hubert", "456"};
|
||||||
|
char strings[][STRING_GROESSE] = {"789", "Zorro", "Adam", "Werner", "123", "Bernd", "Hubert", "456"};
|
||||||
unsigned int anzahlStrings = sizeof(strings)/sizeof(strings[0]);
|
unsigned int anzahlStrings = sizeof(strings)/sizeof(strings[0]);
|
||||||
|
|
||||||
printf("Vor Sortierung:\n");
|
printf("Vor Sortierung:\n");
|
||||||
gibStringsAus(strings, anzahlStrings);
|
gibStringsAus(strings, anzahlStrings);
|
||||||
|
|
||||||
|
qsort(strings, anzahlStrings, sizeof(strings[0]), compare);
|
||||||
|
|
||||||
printf("\nNach Sortierung:\n");
|
printf("\nNach Sortierung:\n");
|
||||||
gibStringsAus(strings, anzahlStrings);
|
gibStringsAus(strings, anzahlStrings);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gibStringsAus(const char *strings[], unsigned int anzahl)
|
/*void gibStringsAus(const char *strings[], unsigned int anzahl)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < anzahl; i++)
|
||||||
|
{
|
||||||
|
printf("%s\n", strings[i]);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
void gibStringsAus(const char strings[][STRING_GROESSE], unsigned int anzahl)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < anzahl; i++)
|
for(int i = 0; i < anzahl; i++)
|
||||||
{
|
{
|
||||||
printf("%s\n", strings[i]);
|
printf("%s\n", strings[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int compare(const void *arg1, const void *arg2)
|
||||||
|
{
|
||||||
|
//const char *string1 = *(const char **)arg1;
|
||||||
|
//const char *string2 = *(const char **)arg2;
|
||||||
|
const char (*zgr1)[STRING_GROESSE] = (char (*)[STRING_GROESSE])arg1;
|
||||||
|
const char (*zgr2)[STRING_GROESSE] = (char (*)[STRING_GROESSE])arg2;
|
||||||
|
const char *string1 = *zgr1;
|
||||||
|
const char *string2 = *zgr2;
|
||||||
|
|
||||||
|
if(!isdigit(string1[0]) && !isdigit(string2[0]))
|
||||||
|
{
|
||||||
|
return strcmp(string1, string2);
|
||||||
|
}
|
||||||
|
else if(isdigit(string1[0]) && isdigit(string2[0]))
|
||||||
|
{
|
||||||
|
return -strcmp(string1, string2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(isdigit(string1[0]))
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user