Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. C Programming

Sorting in Alphabetical Order

Book navigation

  • C Programming Introduction
  • C – Program Code – Basics Part I
  • C – Program Code – Basics Part II
  • Operators in C Programming
  • C – Program Code – Basics Part III
  • C Program Code Basics - Part IV
  • Addition , Subtraction and Multiplication of Matrices
  • Solving Quadratic Equation
  • Sum of sin series
  • Sum of cos series
  • Sum of e series
  • Ascending and Descending order
  • Sorting in Alphabetical Order
  • Factorial of a number
  • Fibonacci Series
  • String Counting
  • String Manipulation
  • Bisection Method
  • Newton Raphson Method
  • Lagrange's Interpolation
By Karthikeyan , 6 September, 2009

Sample C Program to sort the given set of words in alphabetical order


#include
#include
#define MAXITEMS 10
#define MAXCHAR 20
main( )
{
    char string[MAXITEMS][MAXCHAR], dummy[MAXCHAR];
    int  i = 0, j = 0,n;
    printf("\tSORTING IN ALPHABETICAL ORDER\n");
    printf("\t******************************\n");
     /* Reading the list */
    printf ("\n Enter Number of names(Max.%d): ",MAXITEMS);
    scanf("%d", &n);
    while (i < n)
    scanf ("%s", string[i++]);
       /* Sorting begins */
    for (i=1; i < n; i++) /* Outer loop begins */
    {
        for (j=1; j <= n-i ; j++) /*Inner loop begins*/
        {
            if (strcmp (string[j-1], string[j]) > 0)
            {   /* Exchange of contents */
                strcpy (dummy, string[j-1]);
                strcpy (string[j-1], string[j]);
                strcpy (string[j], dummy );
            }
        } /* Inner loop ends */
    } /* Outer loop ends */
    /* Sorting completed */
    printf ("\n\tAlphabetical list \n\n");
    for (i=0; i < n ; i++)
        printf ("%s\n", string[i]);
    printf("\t****************************\n");

getch ();
}
  • Add new comment

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy