I need help solving this problem, if anyone had a similar problem it would help me a lot.
The task is: Enter string, print and sort only letter from string.
I don't know why program breaks when I want to sort charcter in string, also if I try anything with the variable r in the main, for example to print its length the program will break.
Note, the program needs to be done with the functions listed.
Code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 200
#define KV(x) (x)*(x)
char* letter(const char *);
void sort(char *s);
int lenght(char *);
int main()
{
char s[100];
char *r;
printf("Enter string:");
gets(s);
r=letter(s);
sort(r);
printf("%c",r);
free(r);
return 0;
}
char* letter(const char *s)
{
int i;
int j=0;
char *r;
r=(char*)calloc(lenght(s)+1,sizeof(char));
for(i=0;s[i]!=0;i++)
{
if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z')
{
r[j]=s[i];
printf("%c",r[j]);
j++;
}
}
}
void sort(char *s)
{
for(int i=0;i<lenght(s)-1;i++)
{
for(int j=i+1;j<lenght(s);j++)
{
if(s[i]<s[j])
{
char pom=s[i];
s[i]=s[j];
s[j]=pom;
}
}
}
}
int lenght(char *s)
{
int d;
for(d=0;s[d]!=0;d++);
return d;
}
Thanks in advance !