-->

NEB29:Difference between array and struct

Ans:-
Differences between array and struct are given below.                                                                                                                                                
Arraystruct
1.It stores same type of data1. It stores dis-similar type of data.
2.It uses static memory allocation . 2.I uses dynamic memory allocation.
3.It takes less time to access elements.3.It takes more time to access elements.
4.It uses index value to access elements. 4.It takes help of period operator(.) to access elements.
5.It is derived data type. 5. IT is user's defined data type.
6.We can not define array of array.          6.We can define array of structure.
7.Its syntax is                                          
data type identifier[value];








example,int a[20];
7.Its syntax is
struct tag
{datatype member1;
datatype member2;
data type member3;
}variable;
example,
struct det
{char name[100];
char add[40];
int  roll;
}variable;



program to know a matrix is identity or not

// program to know a matrix is identity or not
//to know a matrix is identity or not
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],order,i,j,first=0,second=0;
printf("enter order\n");
scanf("%d",&order);
printf("enter matrix elements\n");
for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
      {
       scanf("%d",&a[i][j]);
      }
  }
printf("the matrix is\n");
for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
      {
       printf("%d",a[i][j]);
      }
    printf("\n");

  }
 for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
      {
       if(i==j)
       {
first=first+1;
}
else
{
second=second+a[i][j];
}

  }
  }

  if(first==order && second==0)
  {
  printf("it is identity matrix\n");
  }
  else
  {
  printf("it is not");
  }

  getch();
  }

program to understand strcmp(), libraray function

//program to understand strcmp(), libraray function
//strcmp() function
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char first[50],sec[50];
int k;
gets(first);
gets(sec);
k=strcmp(first,sec);
printf("k=%d",k);
if(k==0)
  {
    printf("both are same");
    }
else if(k>0)
  {
  printf("first string =%s has more value\n",first);
  }
  else if(k<0)
  {
  printf("second string =%s has more value\n",sec);
  }
  getch();
  }

program to get hcf and lcm of two numbers

//program to get hcf and lcm of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n,n1,lcm,hcf;
printf("enter numbrs\n");
scanf("%d%d",&a,&b);
if(a>b)
{
n=a;
}
else
{
n=b;
}
for(n1=n;n1>=1;n1--)
    {
    if(a%n1==0 && b%n1==0)
     {
     hcf=n1;
     break;
     }
}
printf("hcf=%d",hcf);
printf("lcm=%d",(a*b)/hcf);
getch();
}

program to multiply two matrices of order mxn

// program to multiply two matrices of order 'mxn'
#include <stdio.h>
#include<conio.h>
void main()
{
    int A[3][3], B[3][3], C[3][3];
    int row, col, i, sum;

    /*
     * Reads elements in first matrix from user
     */
    printf("Enter elements in matrix A of size 2x2: \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   scanf("%d", &A[row][col]);
}
    }

    /*
     * Reads elements in second matrix from user
     */
    printf("\nEnter elements in matrix B of size 2x2: \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   scanf("%d", &B[row][col]);
}
    }

    /*
     * Multiplies both matrices A*B
     */
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   sum = 0;
   /*
    * Multiplies row of first matrix to column of second matrix
    * And stores the sum of product of elements in sum.
    */
   for(i=0; i<2; i++)
   {
sum += A[row][i] * B[i][col];
   }

   C[row][col] = sum;
}
    }

    /*
     * Prints the product of matrices
     */
    printf("\nProduct of Matrix A * B = \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   printf("%d ", C[row][col]);
}
printf("\n");
    }
getch();;
}

NEB26:program to reverse a number using function reverse()

//program to reverse a number using function reverse()
#include<stdio.h>
#include<conio.h>
void reverse();
void main()
{
prinf("the output is\n");
reverse();
getch();
}
void reverse()
{
int num;
int rem;
printf("enter a number\n");
scanf("%d",&num);
while(num!=0)
{
 rem=num%10;
    printf("%d",rem);
num=num/10;
}
------------------------
or simply we can also use strrev() function.



NEB24:explanation of string handling functions

Ans-
String--
           It means series of characters used in our program. We can also use digits with characters. They also act as string or a part of it. It is used when we input our name or address or somewhere their combination.

Syntax
char identifier[value];
or char identifier[value][value];

We can use he upper one if we want to input single string then. And if we want to input multiple values then we use lower one(called 2-dimensional array in string).

For example,
char name[100];
gets(name)
Here, the variable accepts value having maximum characters 100. We can also write ,
char name[10]="kathmandu";
IT stores the value in following way.

It stores values from cell location '0' and ends at null character called escape sequence. This is used to show that there is no more character beyond that. This null character is used with strings only. For  numbers, it is not used.
                        Similarly, if we want to store multiple string with same name then one-dimensional is not applicable. We use two dimensional string.
We declare:

char name[5][50];
It means, we are going to store any five names having optimum 50 characters.
Let's look below. If we have five names :RAM,SHYAM,Nita then it would like

so sequentially it stores data in different row. We use loop to execute it with different data.
Or, instead of inputting string, if we initialize strings then it can be written as
  char name[5][50]={"RAM","Shyam","Nitu"};

String handling functions:-There are some in-built functions,  we can say in-built small program. It is used to make our program easier and faster.
      Let's know about some.
1) strlen(string);-It is used to get length of given string.

 syntax:
identifer=strlen(string);
For example,
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
int k;
k=strlen(string);
printf("length=%d",k);
}
It returns 5. It also counts white spaces present in a string.
If we want want , we can also input string using scanf() or gets().

2)strrev():- It reverses the string given by us.

syntax:- strrev(string);
example:

#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
printf("reverseed string=%s",strrev(string));
}

We get output "elppa". Instead of putting in output line, we can put it before printf.

3)strlwr(): This function is used to convert given string into lower case.
syntax:-
strlwr(string);
Example;
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="APPLE";
printf("string in lowercase=%s",strlwr(string));
}

It gives us "apple" as an  output.

4)strupr():This string function is used to convert all characters of  string into upper case.

syntax:
strupr(string);

example:-
#include<stdio.h>
#include<string.h>
void main()
{
char string[100]="apple";
printf("string in uppercase=%s",strupr(string));
}

We get APPLE as an output after execution.

5)strcpy();- We use this function to copy one string to another. while copying, one replaces another.
Syntax:-
strcpy(string1,string2);
or strcpy(destination,source);

When we copy string, this is done from source to destination (string2 to string1). If first is empty then there would not be over-writing.

Example;
#include<stdio.h>
#include<string.h>
void main()
{
char string1[100]="APPLE";
char string2[100]="grapes";
printf("copied string =%s",strcpy(string1,string2));
}

output-
We get here string1=grapes. Because 'grapes' is copied to firs string named "APPLE". We can also write in reverse order.

Like these all, we have many other string handling functions like, strcat(),strchr(),strdup(),strset() etc.