Monday, July 31, 2017

Application of struct in C

#include<stdio.h>
#include<string.h>
struct student
{
    char name[20];
    int roll;
};
void main()
{
  struct student s[10],t;
  int i,n,j;
  printf("Enter no. of students : ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      printf("Enter %dth student's name and roll : ",i+1);
      scanf("%s%d",s[i].name,&s[i].roll);
  }
  for(i=0;i<=n-2;i++)
  {
      for(j=i+1;j<=n-1;j++)
      {
          if(strcmp(s[i].name,s[j].name)>0)
          {
              t=s[i];
              s[i]=s[j];
              s[j]=t;
          }
      }
  }
  printf("\nName sorted in alphabetical order is : ");
  printf("\nName\tRoll No.");
  for(i=0;i<n;i++)
  {
      printf("\n%s\t%d",s[i].name,s[i].roll);
  }
  getch();
}

Related Posts:

  • Sorting names in C#include<stdio.h> //#include<string.h> void copystring(char txt1[], char txt2[]) {     int k;     for(k=0;txt2[k]!='\0';k++)     {         txt1[k]=txt2[k];  … Read More
  • Copying string 2 to string 1#include<stdio.h> void copystr(char [],char []); void main() {     char str1[50],str2[50];     printf("Enter 1st string,str1 : ");     gets(str1);     printf("\nEnter 2nd string,s… Read More
  • Concatenation of two string in C#include<stdio.h> void catstr(char [],char []); void main() {     char str1[50],str2[50];     printf("Enter 1st string (str1) :");     gets(str1);     printf("\nEnter 2nd string (… Read More
  • Count no. of words in a sentence in C#include<stdio.h> void main() {     char str[50],i,wordcount=0;     printf("Enter a line of text : ");     gets(str);     for(i=0;str[i]!='\0';i++)     {     &… Read More
  • Code in C to multiply two matrix#include<stdio.h> void matmultiply(int m[3][3], int n[3][3]); void read(int x[3][3]); //void read2(int y[3][3]); void main() {     int a[3][3],b[3][3],l,w;     printf("\nFor matrix a :-\n");   &… Read More

0 comments:

Post a Comment