Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to perform sorting on words or string values using for loop

Posted By: Jay Brown     Category: C Programming     Views: 3239

Write a program to perform sorting on words or string values using for loop.

Code for Program to perform sorting on words or string values using for loop in C Programming

#include<stdio.h>
#include<conio.h>

#define ITEMS 5
#define MAXCHAR 20

void main()
{
    charstring[ITEMS][MAXCHAR],dummy[MAXCHAR];
    int i=0,j=0;
    clrscr();

    printf("Enter names of %d items \n\n",ITEMS);
    while(i<ITEMS)
        scanf("%s",string[i++]);

    for(i=1;i<ITEMS;i++)
    {
        for(j=1;j<=ITEMS-i;j++)
        {
            if(strcmp(string[j-1],string[j])>0)
            {
                strcpy(dummy,string[j-1]);
                strcpy(string[j-1],string[j]);
                strcpy(string[j],dummy);
            }
        }
    }

    printf("\nAlphabetical list \n\n");
    for(i=0;i<ITEMS;i++)
    {
        printf("%s\n",string[i]);
    }
    getch();
}
  
Share: 



Jay Brown
Jay Brown author of Program to perform sorting on words or string values using for loop is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!