C Program to Sort Id, Name, Address of Student using structure.

 Structure Programm

*Program with POINTER*


#include<stdio.h>

#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student{
int id;
char name[20];
char address[20];
}s1[10],*p;
int main()
{
int i,j,temp,n;
printf("Enter the n items \n");
scanf("%d",&n);
char tempchar[50],tempchar1[50];
printf("Enter the id name and address of 5 students\n");
for(p=s1;p<s1+n;++p)
{
scanf("%d %s %s",&p->id,p->name,p->address);
}
p=s1;
for(i=0;i<n;++i){
for(j=i+1;j<n;++j){
if(strcmp((p+i)->name,(p+j)->name)<0)
{
strcpy(tempchar,(p+i)->name);
strcpy((p+i)->name,(p+j)->name);
strcpy((p+j)->name,tempchar);

strcpy(tempchar1,(p+i)->address);
strcpy((p+i)->address,(p+j)->address);
strcpy((p+j)->address,tempchar1);

        temp=(p+i)->id;
        (p+i)->id=(p+j)->id;
        (p+j)->id=temp;
}
}
}
for(p=s1;p<s1+n;++p){
printf("%d\n %s\n %s\n",p->id,p->name,p->address);
}
}

Comments

Popular posts from this blog

Star Topology

Strings in Python