Condition: "A string consists of words separated by one or more spaces. Rearrange the words in alphabetical order." I managed to do without gaps. But still this is not something, please help.

#include <stdlib.h> #include <string.h> #include <stdio.h> #define N 100 #define M 30 int main(int argc, char* argv[]) { char a[N][M] = { "privet", "menia", "zovyt", "Vladislav" }; int n, i; n = 1; qsort(a, n, sizeof(char[M]), (int(*)(const void *, const void *)) strcmp); for (i = 0; i < n; i++) printf("%s\n", a[i]); return 0; } 

Closed due to the fact that off-topic participants Kromster , LFC , aleksandr barakin , 0xdb , MSDN.WhiteKnight March 29 at 3:08 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, LFC, aleksandr barakin, 0xdb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • four
    "I managed to do without spaces"? Written some rubbish. - AnT

1 answer 1

Well, here is the code on the knee ...

 #include <stdlib.h> #include <string.h> #include <stdio.h> // Сравнение слов - через указатели на указатели int wordsCmp(const void* a, const void* b) { const char * c = *(const char **)a; const char * d = *(const char **)b; return strcmp(c,d); } char * sortIt(const char * str) { // Находим все слова - указатели на них. Их никак не больше половины длины строки. const char ** words = malloc(sizeof(char*)*strlen(str)/2); const char * c = str; int idx = 0; while(*c == ' ') ++c; words[idx++] = c++; for(;*c;++c) if (*c != ' ' && *(c-1) == ' ') words[idx++] = c; // Сортировка массива указателей qsort((void*)words,idx,sizeof(char*),wordsCmp); // Память под новую строку char * s = malloc(strlen(str)+1); char *z = s; *s = 0; // Запись слов в новую строку for(int i = 0; i < idx; ++i) { for(const char * t = words[i]; *t && *t != ' ';) *z++ = *t++; if (i < idx-1) *z++ = ' '; } *z = 0; free(words); return s; } int main() { char str[] = "Sorry I can not write this simple program"; printf("%s\n", str); char * res = sortIt(str); printf("%s\n", res); free(res); } 

Not to say what is beautiful or optimal, but ...

  • I think strtok would have been more beautiful) - pavel
  • @pavel I did not want to change the source line :) - Harry
  • Yes, an interesting approach to spaces, as word limiters. In this case, the same words will be sorted by the number of subsequent spaces (the more there are, the less the word will be when comparing (except the last one in the source line)). By the way, the number of leading spaces before the first source word can be remembered and placed at the beginning of the result string. - avp 8:17 pm