Write a C program to sort the words in a string and the print the sorted string in alphabetical orde
Write a C
for eg
the input is " Love By Allen"
output should be "Allen By Love"
EXPLANATION -
Create a function named with func that is user defined function .
The printf() will display a particular value of that string.
Then the function id defined .
PROGRAM -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(char*);
int main()
{
char s[100];
printf(" Please enter your text\n");
gets(s);
func(s);
printf("%s\n", s);
return 0;
}
void func(char *s)
{
int c, d = 0, l;
char *p, *r, ch;
l = strlen(s);
r = (char*)malloc(l+1);
p = s;
for ( ch = 'a' ; ch <= 'z' ; ch++ )
{
for ( c = 0 ; c < l ; c++ )
{
if ( *p == ch )
{
*(r+d) = *p ;
d++;
}
p++;
}
p = s;
}
*(r+d) = '\0';
strcpy(s, r);
free(r);
}
Step by step
Solved in 3 steps with 1 images