Pages

Tuesday, 19 February 2019

Program to illustrate string manipulation functions - strlen() , strcpy() , strcmp() ,strcat()

/*Program to Illustration of string manipulation functions   - strlen() , strcpy() , strcmp() , strcat() */
#include<stdio.h>
#include<string.h>
void main()
{
   char s1[50],s2[50],s3[50];
   char substr[50];
   int cmp,position,n,i,j,l,m;
   printf("\n Enter string1:");
   scanf("%s",s1);
   l=strlen(s1);
   printf("Lenght of the string  s1 is %d",l); 
   printf("\n\n Enter string2:");
   scanf("%s",s2);
   m=strlen(s2);
   printf("Lenght of the string s2 is %d",m);
   strcpy(s3,s1);
   printf("\n\nAfter copying S1 into S3,the string S3 is %s",s3);
   printf("\nString S1\t:%s",s1);
   printf("\nString S2\t:%s",s2);
   printf("\nString S3\t:%s",s3);
   cmp=strcmp(s1,s2);
   if(cmp==0)
     printf("\nStrings S1 and S2 are equal.");
   else
     printf("\nStrings S1 and S2 are unequal.");
   strcat(s1,s2);
   printf("\n\nAfter concatanation,the string S1 is %s",s1);
   printf("\n\nEnter the position and number of characters you wnat to extract from the concatenated string:");
   scanf("%d %d",&position,&n);
   for(i=position-1, j=0 ; j<n ; i++, j++)
    {
    substr[j]=s1[i];
    }
   substr[j]='\0';
   printf("\nThe substring extracted from string S1 is %s",substr);
  
}

output:


Monday, 18 February 2019

Program to illustrate string functions

String functions : 

string functions used in c are
1.string length  ----- strlen()
2.string copy    -----  strcpy()
3.string reverse ----- strrev()
4.string compare  -----  strcmp()
5.string concatenate  -----  strcat()
6.string upper  -----  strupr()
7.string lower  -----  strlwr()

Program 1: string length  ----- strlen()

// program to illustrate string length ----strlen
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
 {
    char name[20];   
    int l;
    pf("\nenter any string :  ");
    sf("%[^\n]s",name);
    l=strlen(name);
    pf("\nentered name is : %s and its length is : %d",name,l);
}

output:



Program 2: string copy  ----- strcpy()

// program to illustrate string copy ----strcpy
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
 {
    char name1[20],name2[44];   
    pf("\nenter any string in name1 :  ");
    sf("%[^\n]s",name1);
    strcpy(name2,name1);
    pf("\ncopied string in name2 is : %s",name2);
}
output:


Program 3: string reverse  ----- strrev()
  
    // program to illustrate string reverse using strlen()
    #include <stdio.h>
    #include <string.h>
    int main()
    {
      int i=0, j, temp;
      char s[22];
      printf("\n\n Enter any string :");
      scanf("%s", s);
      j=strlen(s) - 1;
      printf("\n\n Entered string is : %s and its length is %d", s,j+1);
      while(i < j)
      {
        temp = s[i];
        s[i++] = s[j];
        s[j--] = temp;
      }
      printf("\n\n Reverse string is: %s", s);
    }

output:


Program 4: string compare  ----- strcmp()

// program to illustrate string compare ----strcmp
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
 {
    char name1[20],name2[44];
    int r;    
    pf("\nenter any string in name1 :  ");
    sf("%[^\n]s",name1);
    pf("\nenter any string in name2 :  ");
    sf("%s",name2);
    r=strcmp(name1,name2);
    if (r == 0)  
        pf("\nBoth the strings are equal");
    else
        pf("\nBoth the strings are not equal");
}

output:


Program 5: string concatenate  ----- strcat()

// program to illustrate string concatenate ----strcat
#include<stdio.h>
#include<string.h>
#define pf printf
#define sf scanf
void main()
 {
    char name1[20],name2[44];
    int r;    
    pf("\nenter any string in name1 :  ");
    sf("%[^\n]s",name1);
    pf("\nenter any string in name2 :  ");
    sf("%s",name2);
    strcat(name1,name2);
    pf("\nconcatenated string is : %s",name1);
 }

output:



Program 6: string upper  ----- strupr()

// program to illustrate string upper ----strupr using functions
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void strupper ( char* );
void main()
 {
  char string[] = {"r k raju"};
  printf( "%s\n", string );
  strupper( string );
  printf( "%s\n", string );
 }

void strupper ( char *p )
{
  while( *p )
  {
   *p=toupper( *p );
   p++;
  }
}

output:


Program 7: string lower  ----- strlwr()

// program to illustrate string upper ----strupr using functions
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void strupper ( char* );
void main()
 {
  char string[] = {"r k raju"};
  printf( "%s\n", string );
  strupper( string );
  printf( "%s\n", string );
 }

void strupper ( char *p )
{
  while( *p )
  {
   *p=tolower( *p );
   p++;
  }
}


output:
 

Program to illustrate Bubble sort

Program to illustrate Bubble sort

Example to illustrate Bubble sort in ascending order
consider an array a[5]
Data    :    16       15      2        13        6
Array  : a[0]      a[1]    a[2]     a[3]    a[4]


Pass 1:

a[0]=16  15   15   15  15
a[1]=15  16   2     2    2
a[2]=2    2    16   13   13
a[3]=13  13  13   16   6
a[4]=6    6    6     6    16

By the end of this pass 1 the highest number 16 will be in a[4]

Pass 2:
a[0]= 15  2    2     2
a[1]= 2    15  13   13
a[2]= 13  13  15   6
a[3]=  6   6    6    15
a[4]= 16  16  16   16
By the end of this pass 2 the second highest number 15 will be in a[3]

Pass 3:
a[0]= 2    2    2
a[1]= 13  13   6
a[2]= 6    6     13
a[3]= 15  15   15
a[4]= 16  16   16
By the end of this pass 3 the third highest number 13 will be in a[2]

Pass 4:
a[0]=  2   2
a[1]=  6   6
a[2]=  13 13
a[3]=  15  15
a[4]=  16  16
By the end of this pass 4 the fourth highest number 6 will be in a[1]


Ascending order sorting :

 // Program to illustrate Bubble sort in ascending order
#include<stdio.h>
void main()
{
 int i,j,a[20],temp,n;
 printf("\n Enter length of the array: \n");
 scanf("%d",&n);
 printf("\n Enter array elements:\n ");
 for(i=0;i<n;i++)
   scanf("%d",&a[i]);
 printf("\n Array elements before Bubble sort :\n");
 for(i=0;i<n;i++)
   printf("\n Address : %p --> Array : a[%d] --> Data : %d",&a[i],i,a[i]);
 for(i=0;i<n;i++)
  {
   for(j=0;j<n-1;j++)
    {
     if(a[j]>a[j+1])
      {
      temp=a[j];
      a[j]=a[j+1];
      a[j+1]=temp;
      }
    }
  }
 printf("\n Array elements after Bubble sort :\n");
 for(i=0;i<n;i++)
   printf("\n Address : %p --> Array : a[%d] --> Data : %d",&a[i],i,a[i]);
}

output:


Descending order sorting :

// Program to illustrate Bubble sort in descending order
#include<stdio.h>
void main()
{
 int i,j,a[20],temp,n;
 printf("\n Enter length of the array: \n");
 scanf("%d",&n);
 printf("\n Enter array elements:\n ");
 for(i=0;i<n;i++)
   scanf("%d",&a[i]);
 printf("\n\n Array elements before Bubble sort :");
 for(i=0;i<n;i++)
   printf("\n Address : %p --> Array : a[%d] --> Data : %d",&a[i],i,a[i]);
 for(i=0;i<n;i++)
  {
   for(j=0;j<n-1;j++)
    {
     if(a[j]<a[j+1])
      {
      temp=a[j];
      a[j]=a[j+1];
      a[j+1]=temp;
      }
    }
  }
 printf("\n\n Array elements after Bubble sort :");
 for(i=0;i<n;i++)
   printf("\n Address : %p --> Array : a[%d] --> Data : %d",&a[i],i,a[i]);
}

output:


Program to illustrate Binary Search


Example to illustrate Binary search 

consider an array[10]
Data:       10     18      19     20     25     30     49      57      64    72
Array:    a[0]   a[1]   a[2]   a[3]   a[4]   a[5]   a[6]   a[7]   a[8]  a[9]
search no = 49

Iteration 1: start = 0
                   end= 9
                   middle = (start+end)/2;
                               = 0 + 9 / 2
                               = 4.5 
                               = rounded 4   i.e  a[4]=25

Iteration 2: search no 49 > 25
                   start = middle + 1 = 4 + 1 = 5
                   end= 9
                   middle = (start+end)/2;
                               = 5 + 9 / 2
                               = 7  i.e   a[7]= 57 

Iteration 3: search no 49 < 57
                   start =  5
                   end= middle  - 1 = 7 - 1 = 6
                   middle = (start+end)/2;
                               = 5 + 6 / 2
                               = 5.5
                               = rounded 5  i.e   a[5]= 30 
     
Iteration 4: search no 49 < 30
                   start = middle + 1 = 5 + 1 = 6
                   end= 6
                   middle = (start+end)/2;
                               = 6 + 6 / 2
                               = 6  i.e   a[6]= 49 
                  
Iteration 5: search no 49  is equal to middle no 49 i.e 49 = 49
                   stop.

Program :

// Program to illustrate Binary Search
    #include <stdio.h>
    int main()
    {
      int array[100],start,end, middle , search , flag, i, n;
      printf("Enter number of elements in array\n");
      scanf("%d", &n);
      printf("Enter %d integer(s)\n", n);
      for (i = 0; i < n; i++)
        {
         scanf("%d", &array[i]);
        }
      printf("\n Array elements are :");
      for (i = 0; i < n; i++)
        {
        printf("\n Address.> :%p --> Array : array[%d] Data : %d Location : %d", &array[i],i,array[i],i+1);
        }
      printf("\nEnter a number to search :");
      scanf("%d", &search);
     
      start = 0;
      end = n - 1;
     
    
      while (start <= end)
       {
          middle = (start+end)/2;
          if (search < array[middle])
             end = middle - 1;   
          else if (search  > array[middle])
             start = middle + 1;
          else if(search == array[middle])
            {
             printf("element %d found at location %d.\n", search, middle+1);
             flag=1;
             break;
            }
       }
       if (flag==0)
          printf("Not found! %d isn't present in the list.\n", search);
    
       return 0; 
    }



      output:

Sunday, 17 February 2019

Program to illustrate Matrix Multiplication

// C program to illustrate matrix multiplication
  #include <stdio.h>
  void main()
    {
       int r1,r2,c1,c2, i, j,k, first[10][10], second[10][10], mul[10][10];
       printf("Enter the number of rows (r1) and columns (c1) of first matrix\n");
       scanf("%d%d", &r1, &c1);
       printf("Enter the number of rows (r2) and columns (c2) of second matrix\n");
       scanf("%d%d", &r2, &c2);
       if(c1 == r2)
       {
       printf("Enter the %d elements of first matrix\n",r1 * c1);
       for (i = 0; i < r1; i++)
          {
          for (j = 0; j < c1; j++)
            {
             scanf("%d", &first[i][j]);
            }
          }
       printf("\nFirst matrix :\n");
       for (i = 0; i < r1; i++)
         {
          for (j = 0; j < c1; j++)
            {
             printf("%d\t", first[i][j]);
            }
           printf("\n");
         }
       printf("Enter the %d elements of second matrix\n",r2 * c2);
       for (i = 0; i < r2; i++)
          {
          for (j = 0; j < c2; j++)
            {
             scanf("%d", &second[i][j]);
            }
          }
       printf("\nSecond matrix : \n");
       for (i = 0; i < r2; i++)
         {
          for (j = 0; j < c2; j++)
            {
             printf("%d\t", second[i][j]);
            }
           printf("\n");
         }
       printf("Matrix Multiplication is :-\n");
       for (i = 0; i < r1; i++)
        {
          for (j = 0 ; j < c2; j++)
          {
          mul[i][j]=0; 
          for (k = 0 ; k < c1; k++)
            {
             mul[i][j] = mul[i][j] + (first[i][k] * second[k][j]);
            }
          }
       }
       for (i = 0; i < r1; i++)
        {
          for (j = 0 ; j < c2; j++)
          {
            printf("%d\t", mul[i][j]);
          }
         printf("\n");
       }
    }
    else
      printf("Matrix multiplication can not perform since r1 is not equal to c2 \n");
 }

output:


Program to illustrate Matrix Addition

// C program to illustrate matrix addition:
  #include <stdio.h>
  void main()
    {
       int row, col, i, j, first[10][10], second[10][10], sum[10][10];
       printf("Enter the number of rows and columns of first matrix\n");
       scanf("%d%d", &row, &col);
       printf("Enter the %d elements of first matrix\n",row * col);
       for (i = 0; i < row; i++)
          {
          for (j = 0; j < col; j++)
            {
             scanf("%d", &first[i][j]);
            }
          }
       printf("\nFirst matrix :\n");
       for (i = 0; i < row; i++)
         {
          for (j = 0; j < col; j++)
            {
             printf("%d\t", first[i][j]);
            }
           printf("\n");
         }
       printf("Enter the number of rows and columns of second matrix\n");
       scanf("%d%d", &row, &col);
       printf("Enter the %d elements of second matrix\n",row * col);
       for (i = 0; i < row; i++)
          {
          for (j = 0; j < col; j++)
            {
             scanf("%d", &second[i][j]);
            }
          }
       printf("\nSecond matrix : \n");
       for (i = 0; i < row; i++)
         {
          for (j = 0; j < col; j++)
            {
             printf("%d\t", second[i][j]);
            }
           printf("\n");
         }
       printf("Sum of entered matrices:-\n");
       for (i = 0; i < row; i++)
        {
          for (j = 0 ; j < col; j++)
          {
             sum[i][j] = first[i][j] + second[i][j];
             printf("%d\t", sum[i][j]);
          }
          printf("\n");
       }
    }

output:

Program to illustrate Linear Search

// Program to illustrate Linear Search
    #include <stdio.h>
    int main()
    {
      int array[100], search, i, n;
      printf("Enter number of elements in array\n");
      scanf("%d", &n);
      printf("Enter %d integer(s)\n", n);
      for (i = 0; i < n; i++)
        {
         scanf("%d", &array[i]);
        }
      printf("\n Array elements are :");
      for (i = 0; i < n; i++)
        {
        printf("\n Address :%p --> Array : array[%d] Data : %d Location : %d", &array[i],i,array[i],i+1);
        }
      printf("\nEnter a number to search :");
      scanf("%d", &search);
      for (i = 0; i < n; i++)
      {
        if (array[i] == search)    /* If required element is found */
        {
          printf("%d is present at location %d.\n", search, i+1);
          break;
        }
      }
      if (i == n)
        printf("%d isn't present in the array.\n", search);
      return 0;
    }

output:

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...