Pages

Sunday, March 31, 2019

Nested Structure in C

C programming provides important feature called nesting of Structure. A structure can be nested inside another structure. This feature is useful to maintain complex data structure. Following example explains how a Person structure is used in a Student structure.

Nesting of Structure in C

#include<stdio.h>

//Declaring a structure student
struct student
{
    struct person
    {
        char firstname[20];
        char lastname[20];
        char dob[10];
    } p ;

    int rollno;
    float marks;
};

int main()
{
    struct student s1;

    printf("*** READING STUDENT'S DETAILS *** \n\n");

    printf("Enter firstname: ");
    scanf("%s", s1.p.firstname);

    printf("Enter lastname: ");
    scanf("%s", s1.p.lastname);

    printf("Enter DoB(dd-mm-yyyy): ");
    scanf("%s", s1.p.dob);

    printf("Enter roll no: ");
    scanf("%d", &s1.rollno);

    printf("Enter marks: ");
    scanf("%f", &s1.marks);

    printf("\n*** PRINTING DETAILS OF STUDENT FROM STRUCTURE ***\n\n");

    printf("Name: %s %s\n", s1.p.firstname, s1.p.lastname);
    printf("DOB: %s\n", s1.p.dob);
    printf("Roll no: %d\n", s1.rollno);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

Output of Program

*** READING STUDENT'S DETAILS ***

Enter firstname: Purv
Enter lastname: Patel
Enter DoB(dd-mm-yyyy): 10-12-2008
Enter roll no: 101
Enter marks: 85

*** PRINTING DETAILS OF STUDENT FROM STRUCTURE ***

Name: Purv Patel
DOB: 10-12-2008
Roll no: 101
Marks: 85.00


* * * * *

Recommended Readings: C Structure



Sunday, March 17, 2019

Caesar Cipher - Decryption Program


Caesar Cipher - Decryption Program

This program will decrypt a given encrypted (Cipher Text) file using Caesar Cipher Decryption Cryptography Algorithm.

Assumptions: 

  • Caesar cipher key used is 3.
  • Input file name is: cipher.txt
  • Output file will be plaintext.txt


#include<stdio.h>
int main() {
   FILE *inputFile, *outputFile;
   char ch;

   inputFile = fopen("cipher.txt", "r");
   if (inputFile == NULL) {
      puts("File cipher.txt Read Error.");
      exit(1);
   }

   outputFile = fopen("plaintext.txt", "w");
   if (outputFile == NULL) {
      puts("File plaintext.txt Write Error.");
      exit(1);
   }

   do {
      ch = fgetc(inputFile);
      fputc(ch - 3, outputFile);
   } while (ch != EOF);
   
  printf("cipher.txt file is successfully decrypted using Caesar Cipher.\n");
  printf("plaintext.txt file is generated successfully.");
  
  fclose(inputFile);
  fclose(outputFile);
  
  return 0;
}

Output of program

cipher.txt file is successfully decrypted using Caesar Cipher.
plaintext.txt file is generated successfully.

Notes: This program will deduct 3 from ASCII value of each character of a given cipher text data file. A sample execution data is given below:

Original content of cipher.txt input file before program execution:

Zhofrph#wr#F#Surjudp#Sudfwlfdov1Eorjvsrw1Frp
Wklv#lv#Fdhvdu#Flskhu#Hqfu|swlrq#Ghprqvwudwlrq1

Content of plaintext.txt file after decryption using program:

Welcome to C Program Practicals.Blogspot.Com
This is Caesar Cipher Encryption Demonstration.



Recommended Readings: Computer Security Practicals

Caesar Cipher - File Encryption Program


Caesar Cipher - Encryption Program

This program will encrypt a given Data file using Caesar Cipher Cryptography Algorithm.

Assumptions: 




  • Caesar cipher key used is 3.
  • Input file name is: data.txt
  • Output file will be output.txt

#include<stdio.h>
int main() {
   FILE *inputFile, *outputFile;
   char ch;

   inputFile = fopen("data.txt", "r");
   if (inputFile == NULL) {
      puts("File data.txt Open Error.");
      exit(1);
   }

   outputFile = fopen("output.txt", "w");
   if (outputFile == NULL) {
      puts("File output.txt Open Error.");
      exit(1);
   }

   do {
      ch = fgetc(inputFile);
      fputc(ch+3, outputFile);
   } while (ch != EOF);
   
  printf("Data.txt file is successfully encrypted using Caesar Cipher.\n");
  printf("Output.txt file is generated successfully.");
  
  fclose(inputFile);
  fclose(outputFile);
  
  return 0;
}


Output of program


Data.txt file is successfully encrypted using Caesar Cipher.
Output.txt file is generated successfully.

Notes: This program will add 3 to ASCII value of each character of a given data file. A sample execution data is given below:

Original content of Data.txt input file before program execution:

Welcome to C Program Practicals.Blogspot.Com
This is Caesar Cipher Encryption Demonstration.

Content of Output.txt file after program encryption:

Zhofrph#wr#F#Surjudp#Sudfwlfdov1Eorjvsrw1Frp
Wklv#lv#Fdhvdu#Flskhu#Hqfu|swlrq#Ghprqvwudwlrq1


Recommended Readings: Computer Security Practicals


* * * * *


Sunday, March 3, 2019

Program to find execution time.


Program to find execution time.

This program will find total CPU execution time taken by a for loop of a program.

#include<stdio.h>
#include<time.h>
int main() 
{
int i;
float executionTime;
clock_t startTime, endTime;

startTime = clock();
//Get Time value before for loop execution 

for (i = 0; i < 100; i++) {
printf("%d, ", i);
}

endTime = clock();
//Get Time after for loop execution 

executionTime = ((float) (endTime - startTime)) / CLOCKS_PER_SEC;
//calulate total time

printf("\n\nTime taken to execute \"for loop\" 100 times is: %f seconds.", executionTime);
return 0;
}

Output of program

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,

Time taken to execute "for loop" 100 times is: 0.016000 seconds.



Recommended Readings: C Program Practicals / Special C Programs