i know
it should stop copying when it gets a space
because its %s
but in this fixed
it copies the whole thing
??
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node node;
struct node{
int value;
struct node * next;
};
int countletters(char *str);
char * sortbycount(char* str);
void main()
{
int g;
char str[18]="aabx bXcb bBxaDAa";
char * r;
g=countletters(str);
r=sortbycount(str);
}
int countletters(char *str)
{
int i,cnt=0;
int ch[26]={0};
for (i=0;i<(int)strlen(str);i++)
{
if ((str[i]>='a')&&(str[i]<='z'))
{
ch[str[i]-'a']=1;
}
if ((str[i]>='A')&&(str[i]<='Z'))
{
ch[str[i]-'A']=1;
}
}
for (i=0;i<26;i++)
{
if(ch[i]==1){ cnt++;}
}
printf("%d",cnt);
return cnt;
}
char * sortbycount(char* str)
{
int start=0,t=0,i,cnt=0;char** arr;
for (i=0;i<(int)strlen(str);i++)
{
if (str[i]==' ') cnt++;
}
arr=(char**)malloc(sizeof(char*)*(cnt+1));
i=0;
do
{
for (i=start;str[i]!=' ';i++);
arr[t]=(char*)malloc(sizeof(char)*(i-start));
sprintf(arr[t],"%s",str+start);
start=i;
t++;
}while(t<cnt);
return str;
}