Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Friday, May 30, 2014

C program to sort a string in alphabetic order

C program to sort a string in alphabetic order: For example if user will enter a string "programming" then output will be "aggimmnoprr" or output string will contain characters in alphabetical order.

C programming code

#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
void sort_string(char*);
void main()
{
   char string[100];
   clrscr();
   printf("Enter some text\n");
   gets(string);
   sort_string(string);
   printf("%s\n", string);
   getch();
}
void sort_string(char *s)
{
   int c, d = 0, length;
   char *pointer, *result, ch;
   length = strlen(s);
   result = (char*)malloc(length+1);
   pointer = s;
   for ( ch = 'a' ; ch <= 'z' ; ch++ )
   {
      for ( c = 0 ; c < length ; c++ )
      {
         if ( *pointer == ch )
         {
            *(result+d) = *pointer;
            d++;
         }
         pointer++;
      }
      pointer = s;
   }
   *(result+d) = '\0';
   strcpy(s, result);
   free(result);
}

Output of program:



No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC