Write a  C program to sort the words in a string and the print the sorted string in alphabetical orde

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter9: Completing The Basics
Section9.4: Character Manipulation Functions
Problem 3E
icon
Related questions
Question

Write a  C program to sort the words in a string and the print the sorted string in alphabetical order 
for eg 
the input is " Love By Allen"

output should be "Allen By Love"

Expert Solution
Step 1

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;
}

 

Step 2

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);
}

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Bare Bones Programming Language
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr