You are viewing a single comment's thread. Return to all comments →
without using any c stdlibrary
include <string.h> int lexicographic_sort(const char* a, const char* b) { int i = 0; while (a[i] != '\0' || b[i] != '\0'){ if(a[i] > b[i]) { return 1; } else if (a[i] == b[i]) i++; else { return 0; } } return 0; } int lexicographic_sort_reverse(const char* a, const char* b) { int i = 0; while (a[i] != '\0' || b[i] != '\0'){ if(a[i] < b[i]) { return 1; } else if (a[i] == b[i]) i++; else { return 0; } } return 0; } int sort_by_number_of_distinct_characters(const char* a, const char* b) { int charA[26]={0},countA = 0; int charB[26]={0},countB = 0; int i = 0; const char *temp; while (a[i] != '\0' || b[i] != '\0'){ charA[a[i] - 'a']++ ; charB[b[i] - 'a']++ ; i++; } for(int i=0;i<26;i++){ if(charA[i] != 0) countA++; if(charB[i] != 0) countB++; } if(countA > countB){ return 1; } else if (countA == countB){ if(lexicographic_sort(a,b) == 1) return 1; } return 0; } int sort_by_length(const char* a, const char* b) { char *temp; if(strlen(a) == strlen(b)){ if(lexicographic_sort(a,b)) return 1; } else if(strlen(a) > strlen(b)){ return 1; } return 0; } void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){ for (int i = 0 ; i < len-1; i++){ for(int j = 0;j<len-1-i ; j++){ //char *a = *(arr+ i); //char *b = *(arr+ i + 1); //printf("before a=%s b=%s \n",arr[j],arr[j+1]); if((*cmp_func)(arr[j],arr[j+1])){ char *temp; temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; //printf("after a=%s b=%s \n",arr[j],arr[j+1]); } } //for(int j = 0 ; j < len;j++) // printf("%s ",arr[j]); //printf("\n"); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting Array of Strings
You are viewing a single comment's thread. Return to all comments →
without using any c stdlibrary