Thursday, 11 July 2019

C Program for Array of Structures

Create a database using an array of structures and perform the following operations on it
1. Create 2. Add 3. Search 4. Update 5. Delete


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
      int rno,marks,del;
      char name[20];
};

void create(int,struct student []);
void add(int,struct student []);
void display(int,struct student []);
void search(int,int,struct student []);
void update(int,int,struct student []);
void delet(int,int,struct student []);

 int main()
{
      setbuf(stdout, NULL);
      int n,value,choice;

      struct student s[10];
      setbuf(stdout, NULL);
      printf("\n\t\t**STUDENT DATABASE**\n");
      printf("\n\tEnter the number of students >");
      scanf("%d",&n);
      create(n,s);

      do
      {
      printf("\n\n----------------------------MENU----------------------------");
      printf("\n\t1:ADD\n\t2:Display\n\t3:Search\n\t4:Update\n\t5:Delete\n\t\n\n\tEnter your choice\t>>");
      scanf("%d",&choice);
      printf("------------------------------------------------------------  \n");

      switch(choice)
      {
      case 1:
            n=n+1;
            add(n,s);
            break;
      case 2:
            printf("\n\tRollNo\tNAME\tMARKS \n");
            display(n,s);
            break;
      case 3:
            printf("\n\tEnter the RollNo. to Search   >");
            scanf("%d",&value);
            search(n,value,s);
            break;
      case 4:
            printf("\n\tEnter the RollNo. to Update   >");
            scanf("%d",&value);
            update(n,value,s);
            break;
      case 5:
            printf("\n\tEnter the RollNo. to Delete   >");
            scanf("%d",&value);
            delet(n,value,s);
            break;

      }
      }while(choice!=0);

      printf("\n");
      return 0;
}

void create(int n,struct student s[10])
{
      int i,j;
      for (i=0;i<n;i++)
      {
      printf("\n\tEnter the RollNo. >>");
      scanf("%d",&s[i].rno);
      printf("\tEnter NAME  >>");
      scanf("%s",s[i].name);
      printf("\tEnter Marks  >>");
      scanf("%d",&s[i].marks);
      s[i].del=0;
      }
}
void add(int n,struct student s[10])
{
      int i,j;
      for (i=n-1;i<n;i++)
      {
      printf("\n\tEnter the RollNo. >>");
      scanf("%d",&s[i].rno);
      printf("\tEnter NAME  >>");
      scanf("%s",s[i].name);
      printf("\tEnter Marks  >>");
      scanf("%d",&s[i].marks);
      s[i].del=0;
      }    
}
void display(int n,struct student s[10])
{
      int i;
      for (i=0;i<n;i++)
      {
            if(s[i].del==0)
                  printf("\n\t%d\t%s\t%d",s[i].rno,s[i].name,s[i].marks);
      }
}
void search(int n,int value,struct student s[10])
{
      int i;
      for (i=0;i<n;i++)
      {
            if (value==s[i].rno)
            {
            printf("\n\tRollNo\tMARKS\tNAME ");
            printf("\n\t%d\t%s\t%d",s[i].rno,s[i].name,s[i].marks);
            break;
            }
      }
      if (i==n)
          printf("\n\tNO RECORD FOUND");
}
void update(int n,int value,struct student s[10])
{
      int i,j;
      for (i=0;i<n;i++)
      {
            if (value==s[i].rno)
            {
            printf("\n\tEnter the RollNo. & NAME & MARKS of student  \n\t");
            scanf("%d%s%d",&s[i].rno,s[i].name,&s[i].marks);
            break;
            }
      }
      if (i==n)
          printf("\n\tNO RECORD TO UPDATE ");  
}
void delet(int n,int value,struct student s[10])
{
      int i;
      for (i=0;i<n;i++)
      {
            if (value==s[i].rno)
            {
            printf("\n\t----------------------");
            printf("\n\tRollNo\tMARKS\tNAME ");
            printf("\n\t%d\t%s\t%d",s[i].rno,s[i].name,s[i].marks);

            printf("\n\t----------------------");
            printf("\n\tSure to delete: 1=YES/2=NO  >");
            scanf("%d",&s[i].del);
            break;
            }
      }
}

Output:
**STUDENT DATABASE** Enter the number of students >2 Enter the RollNo. >>1 Enter NAME  >>a Enter Marks  >>11 Enter the RollNo. >>2 Enter NAME  >>b Enter Marks  >>22
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>1------------------------------------------------------------   Enter the RollNo. >>3 Enter NAME  >>c Enter Marks  >>33
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>2------------------------------------------------------------   RollNo NAME MARKS  1 a 11 2 b 22 3 c 33----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>3------------------------------------------------------------   Enter the RollNo. to Search >2 RollNo MARKS NAME  2 b 22----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>4------------------------------------------------------------   Enter the RollNo. to Update >3 Enter the RollNo. & NAME & MARKS of student   3cc33
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>2------------------------------------------------------------   RollNo NAME MARKS  1 a 11 2 b 22 3 cc 33----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>5------------------------------------------------------------   Enter the RollNo. to Delete >3 ---------------------- RollNo MARKS NAME  3 cc 33 ---------------------- Sure to delete: 1=YES/2=NO  >1
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>2------------------------------------------------------------   RollNo NAME MARKS  1 a 11 2 b 22----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>

Wednesday, 10 July 2019

C program for operation on String

Implement following operations on the string (without library string)
1. Length 2. Palindrome 3. Comparison 4. Copy 5. Reverse      6. Substring

#include <stdio.h>

void substr(char str1[], char str2[]);
int palindrome(char str[]);
int compare(char str1[], char str2[]);
void copy(char str1[], char str2[]);
void reverse(char str[]);
int main()
{
      setbuf(stdout, NULL);
      char str1[100], str2[100];
      int result, option;
      do {
            printf("\n1.Substring Searching");
            printf("\n2.Check for Palindrome");
            printf("\n3.String Comparison");
            printf("\n4.Copy string");
            printf("\n5.Reverse String");
            printf("\n6.Quit");
            printf("\n\nEnter Your Choice:");
            scanf("%d", &option);
            switch (option)
            {
            case 1:
                  printf("\nEnter 1st string:");
                  scanf("%s",&str1);
                  printf("\nEnter 2nd string:");
                  scanf("%s",&str2);
                  substr(str1, str2);

                  break;
            case 2:
                  printf("\nEnter a String:");
                  scanf("%s",&str1);
                  result = palindrome(str1);
                  if (result == 0)
                        printf("\nNot a palindrome");
                  else
                        printf("\nIt is palindrome");

                  break;
            case 3:
                  printf("\nEnter 1st string:");
                  scanf("%s",&str1);
                  printf("\nEnter 2nd string:");
                  scanf("%s",&str2);
                  result = compare(str1, str2);
                  if (result == 0)
                        printf("\nboth are same");
                  else if (result > 0)
                        printf("\n1st>2nd");
                  else
                        printf("\n1st<2nd");

                  break;
                  case 4:
                        printf("\nEnter a String:");
                        scanf("%s",&str1);
                        copy(str2, str1);
                        printf("\nResult=%s", str2);

                        break;
                  case 5:
                        printf("\nEnter a String:");scanf("%s",&str1);
                        reverse(str1);
                        printf("\nResult=%s", str1);

                        break;
                  default:
                        printf("\nInvalid Choice:");
                        break;
            }
      } while (option != 6);
      return 0;
}

void substr(char str1[], char str2[])
{
      int i, j, lena, lenb;
      for (lena = 0; str1[lena] != '\0'; lena++);
      for (lenb = 0; str2[lenb] != '\0'; lenb++);
      for (i = 0; i <= lena - lenb + 1; i++) {
            for (j = 0; str1[i + j] == str2[j] && str2[j] != '\0'; j++);
            if (str2[j] == '\0')
                  printf("\nString found at location : %d", i + 1);
      }
}

int palindrome(char str[])
{
      int i, j;
      i = j = 0;
      while (str[j] != '\0')
            j++;
      while (i < j) {
            if (str[i] != str[j - 1])
                  return (0);
            i++;
            j--;
      }
      return (1);
}

int compare(char str1[], char str2[])
{
      int i;
      i = 0;
      while (str1[i] != '\0') {
            if (str1[i] > str2[i])
                  return (1);
            if (str1[i] < str2[i])
                  return (-1);
            i++;
      }
      return (0);
}

void copy(char str2[], char str1[])
{
      int i = 0;
      while (str1[i] != '\0') {
            str2[i] = str1[i];
            i++;
      }
      str2[i] = '\0';
}

void reverse(char str[])
{
      int i, j;
      char temp;
      i = j = 0;
      while (str[j] != '\0')
            j++;
      j--;
      while (i < j) {
            temp = str[i];
            str[i] = str[j];
            str[j] = temp;
            i++;
            j--;
      }
}

Output
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:1

Enter 1st string:madam

Enter 2nd string:am

String found at location : 4
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:2

 Enter a String:madam

A palindrome
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:3

Enter 1st string:Mam

Enter 2nd string:mam

1st<2nd
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:4

Enter a String:hello

Result=hello
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:5

Enter a String:hello

Result=olleh
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:6

Invalid Choice:

Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...