Sunday, February 5, 2017

Simple Railfense - Encryption


//Simple Railfense Technique - Sender side encryption logic.
#include<stdio.h>
int main()
{
  char str[20], str1[10]="", str2[10]="";
  int i, cnt1=0, cnt2=0;

  printf("Enter your plain text:");
  gets(str);

  for(i=0; i<strlen(str); i++)
  {
  if( i%2 == 0)
  {
str1[cnt1++]=str[i];
  }
  else
str2[cnt2++]=str[i];
  }
  printf("Encrypted Text = %s%s",str1,str2);
  return 0;
}

Output of the program

Enter your plain text:HelloStudent
Encrypted Text = HlotdnelSuet



Saturday, February 4, 2017

ARP Simulation

This program will find Physical Address for the given IP address. 


/* Sample arp.txt file generated using arp -a command
  192.168.3.3           f4-f4-9c-18-bb-4b     dynamic   
  192.168.3.2           ca-f8-87-a3-ee-20     dynamic   
  192.168.90.255        ff-ff-ff-ff-ff-ff     static    
  221.0.0.251           03-01-ef-00-ff-fc     static    
  29.253.251.20         02-01-ed-ff-00-fa     static
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
  FILE *f1, *fopen();
  int ch;
  char str1[80], *token;
  const char str[80] = "", s[2] = " ";
  f1 = fopen("arp.txt","r");
  if ( f1 == NULL )     /* check does file exist*/ 
 
printf("Cannot open file for reading \n" ); 
exit(1);    
 
  while(fgets(str1,80,f1)!=NULL){
  token = strtok(str1,s);
  while (token != NULL){
      if (strcmp(token,"192.168.3.2") ==0)
  {
  token = strtok(NULL, s);
  printf("Your Physical address is %s",token);
      }
      else
      token = strtok(NULL, s);
    }
  }
  fclose(f1);
  return 0;
}

Output of the program

Your Physical address is ca-f8-87-a3-ee-20


String N Copy Example

Following example will copy N characters from str2 to str1.

#include <stdio.h> 
#include <string.h> 
int main () 

char str1[10] = "12345"; 
char str2[10] = "World"; 

strncpy(str1, str2, 4); 
str1[4]='\0';

printf("strncpy( str1, str2, 3) = %s\n", str1 ); 

return 0; 
}

Output of the program

strncpy( str1, str2, 3) = Worl


* * * * *

Thursday, February 2, 2017

Merge Array


Read two one dimensional arrays of five elements. Merge both the arrays into third array and display the same.


#include<stdio.h>
int main()
{
int i,count=0, arr1[5], arr2[5], arr3[10];
//Read array1..
for(i=0; i<5; i++)
{
printf("Enter arr1[%d]: ",i);
scanf("%d",&arr1[i]);
}
printf("\n");
//Read array2..
for(i=0; i<5; i++)
{
printf("Enter arr2[%d]: ",i);
scanf("%d",&arr2[i]);
}
//Copy arr1 into arr3...
for(i=0; i<5; i++)
{
arr3[i] = arr1[i];
}
//Append arr2 into arr1..
for(i=0;i<5;i++)
{
arr3[i+5]=arr2[i];
}
printf("Elements in Array 3 are :\n");
for(i=0;i<10;i++)
{
printf("Arr3[%d] = %d \n", i, arr3[i]);
}
}

Output of the program:

Enter arr1[0]: 1
Enter arr1[1]: 2
Enter arr1[2]: 3
Enter arr1[3]: 4
Enter arr1[4]: 5

Enter arr2[0]: 6
Enter arr2[1]: 7
Enter arr2[2]: 8
Enter arr2[3]: 9
Enter arr2[4]: 10
Elements in Array 3 are :
Arr3[0] = 1
Arr3[1] = 2
Arr3[2] = 3
Arr3[3] = 4
Arr3[4] = 5
Arr3[5] = 6
Arr3[6] = 7
Arr3[7] = 8
Arr3[8] = 9
Arr3[9] = 10

* * * * *

Average of Array Elements

Find average of Array Elements.

Example: This program will find avg. of height of five students.



#include<stdio.h>
int main()
{
int i;
float avg_height,sum=0,height[5];
//Logic to read height of 5 students
printf("Enter height in foot.\n");
for(i=0; i<5; i++){
printf("Height of student%d:",i+1);
scanf("%f", &height[i]);
}
for(i=0; i<5; i++){
sum = sum + height[i];
}
avg_height = sum/5;
printf("Average height of 5 students = %.2f",avg_height);
return 0;
}

Output of program

Enter height in foot.
Height of student1:5.5
Height of student2:5.9
Height of student3:5.3
Height of student4:6.2
Height of student5:5.4
Average height of 5 students = 5.66

* * * * *

Wednesday, February 1, 2017

Transpose Matrix

C program to obtain Transpose of a given 3X3 Matrix.


#include<stdio.h>
int main()
{
 int arr[3][3], arrTrans[3][3];
 int i,j;
 //Logic for reading matrix from user...
 printf("Enter the 3x3 matrix:\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter the element arr[%d][%d] : ",i,j);
   scanf("%d",&arr[i][j]);
  }
 }

 printf("The Original matrix is: \n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t%d",arr[i][j]);
  }
  printf("\n");
 }

 printf("Press any key to print Transposed Matrix..\n");
 getch();
//Logic for transposing matrix...
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     arrTrans[j][i] = arr[i][j];
 }
 //Logic for printing Transposed matrix...
 printf("The transpose of the matrix is: \n");
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     printf("\t%d",arrTrans[i][j]);
  printf("\n");
 }
 return 0;
}

Output of program

Enter the 3x3 matrix:
Enter the element arr[0][0] : 1
Enter the element arr[0][1] : 2
Enter the element arr[0][2] : 3
Enter the element arr[1][0] : 4
Enter the element arr[1][1] : 5
Enter the element arr[1][2] : 6
Enter the element arr[2][0] : 7
Enter the element arr[2][1] : 8
Enter the element arr[2][2] : 9
The Original matrix is:
        1       2       3
        4       5       6
        7       8       9
Press any key to print Transposed Matrix..
The transpose of the matrix is:
        1       4       7
        2       5       8
        3       6       9

*/

2D Array Search

C program to search an element in to 2D Array.




#include<stdio.h>
int main()
{
int A[3][3], i, j, number, flag=0;
for (i=0; i<3; i++) {
for(j=0; j<3; j++)
{
printf("Enter your element[%d][%d]:",i,j);
scanf("%d", &A[i][j]);
}
}
printf("Your array is:\n");
for (i=0; i<3; i++) {
for(j=0; j<3; j++)
printf("%d ",A[i][j]);
printf("\n");
}
printf("Enter the value to be search:");
scanf("%d",&number);

for (i=0; i<3; i++) {
for(j=0; j<3; j++)
{
if(A[i][j]==number)
{
flag=1;
break;//exit(0);
}
}
}
if (flag==1)
printf("%d: Present in Array.",number);
else
printf("%d: Not in Array", number);
return 0;
}

Output - 1

Enter your element[0][0]:1
Enter your element[0][1]:2
Enter your element[0][2]:3
Enter your element[1][0]:4
Enter your element[1][1]:5
Enter your element[1][2]:6
Enter your element[2][0]:7
Enter your element[2][1]:8
Enter your element[2][2]:9
Your array is:
1 2 3
4 5 6
7 8 9
Enter the value to be search:5
5: Present in Array.

Output - 2

Enter your element[0][0]:1
Enter your element[0][1]:2
Enter your element[0][2]:3
Enter your element[1][0]:4
Enter your element[1][1]:5
Enter your element[1][2]:6
Enter your element[2][0]:7
Enter your element[2][1]:8
Enter your element[2][2]:9
Your array is:
1 2 3
4 5 6
7 8 9
Enter the value to be search:0
0: Not in Array


* * * * *