C Programming

Write a program to read different names and addresses into the computer and sort them on the basis of names into alphabetical order.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20

struct alphabetic
{
    char name[50];
    char address[100];
}s[MAX], temp;

int main(void)
{
    int i, j, index;
    char t[100], min;
    for(i=0; i<MAX; i++)
    {
        printf("\nEnter name: ");
        scanf("%s", s[i].name);

        printf("Enter address: ");
        scanf("%s", s[i].address);

    }
    printf("/-----------------------------------------------------\n");
    printf("Name\tAddress\n");
    for(i=0; i<MAX; i++)
    {
        for(j=i+1; j<MAX; j++)
        {
            if(strcmp(s[j].name, s[i].name)<0)
            {
                temp = s[j];
                s[j] = s[i];
                s[i] = temp;
            }
        }
        printf("%s\t%s\n", s[i].name, s[i].address);
    }

    printf("/------------------------------------------------------\n");

    return 0;
}

Output

Enter name: Niranjan
Enter address: Janakpur

Enter name: Mahi
Enter address: Jaleshword

Enter name: Amit
Enter address: India

Enter name: Rahul
Enter address: Pokhara

Enter name: Mani
Enter address: Jaleshwor

Enter name: Bikas
Enter address: Rampur

Enter name: Navin
Enter address: pokhriya

Enter name: Rasid
Enter address: America

Enter name: Unaidu
Enter address: Usa

Enter name: Mantoriya
Enter address: Parsa

Enter name: Manish
Enter address: Mumbai

Enter name: Balram
Enter address: Mumbai

Enter name: Avinish
Enter address: Rampura

Enter name: Manu
Enter address: Sorya

Enter name: Amint
Enter address: Pokhra

Enter name: Anitpo
Enter address: Mantop

Enter name: Janup
Enter address: Kolkata

Enter name: Jyoti
Enter address: kamiriya

Enter name: Sonam
Enter address: Mombai

Enter name: Amir
Enter address: Mumbai
/—————————————————–
Name Address
Amint Pokhra
Amir Mumbai
Amit India
Anitpo Mantop
Avinish Rampura
Balram Mumbai
Bikas Rampur
Janup Kolkata
Jyoti kamiriya
Mahi Jaleshword
Mani Jaleshwor
Manish Mumbai
Mantoriya Parsa
Manu Sorya
Navin pokhriya
Niranjan Janakpur
Rahul Pokhara
Rasid America
Sonam Mombai
Unaidu Usa
/——————————————————


image_pdfOpen as Pdfimage_printPrint

Leave a Reply

Your email address will not be published. Required fields are marked *