i need to build a function sortbycount(char* str) uses "countletters" function in order to know which word has a unique number of chars
in a word.
and returns a string in which the words are sorted by ascending order of words
regardind their "countletters" value of this word.
for example:
"ababa abcd abbbbA dbBdc abbcCc"
returns
"abcd abbcCc dbBdc ababa abbbbA"
i got to find the shortest way to do that.
my main problem is the sorting parting part.
if the first word will be 5
and the next will be 10
and the next will return 15
i will need to push the previus words to the second place and put it into the first
and i can have a million words
how can i know in advance which word to put where
here is the countletters code
- Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node node;
struct node{
int value;
struct node * next;
};
int countletters(char *str);
void main()
{
int g;
char str[18]="aabx bXcb bBxaDAa";
g=countletters(str);
}
int countletters(char *str)
{
char ch='a';
char *st;
int cnt=0;
for(;ch<='z';ch++)
{
for(st=str;*st!='\0';st++)
{
if (*st==ch)
{
cnt++;
break;
}
if ((*st>='A')&&(*st<='Z'))
{
if (ch==(*st+32))
{
cnt++;
break;
}
}
}
}
return cnt;
}
