Wednesday, March 15, 2023

C program to decrypt a text file using Caesar Cipher

Caesar Cipher File Decryptor


This C program will decrypt a given text file into plain text file using Caesar Cipher cryptographic algorithm.


#include <stdio.h>
#define MAX_FILE_SIZE 1000

//function to decrypt message based on given key
void decrypt(char *message, int key) {
    char ch;
    int i;
    for(i = 0; message[i] != '\0'; ++i){
        ch = message[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = ch - key;
            if(ch < 'a'){
                ch = ch + 'z' - 'a' + 1;
            }
            message[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = ch - key;
            if(ch < 'A'){
                ch = ch + 'Z' - 'A' + 1;
            }
            message[i] = ch;
        }
    }
}

int main() {
    FILE *fileIn, *fileOut;
    char message[MAX_FILE_SIZE];
    int key;

    // Open the encrypted file
    fileIn = fopen("demo1.txt", "r");
    if(fileIn == NULL){
        printf("File open error.\n");
        return 0;
    }

    // Read the encrypted message from given file
    fgets(message, MAX_FILE_SIZE, fileIn);

    // Close the input file
    fclose(fileIn);

    // Prompt user to enter the key
    printf("Enter key: ");
    scanf("%d", &key);

    // Decrypt the message
    decrypt(message, key);

    // Open the output file
    fileOut = fopen("decrypted.txt", "w");
    if(fileOut == NULL){
        printf("File write error.\n");
        return 0;
    }

    // Write the decrypted message to output file
    fprintf(fileOut, "%s", message);

    // Close the output file
    fclose(fileOut);

    printf("File decrypted successfully.\n");

    return 0;
}

Thursday, February 23, 2023

C File Management Important MCQs

C Programming File Management Important MCQs


1. Which function is used to open a file in C programming language?

(a) fopen()

(b) open()

(c) read()

(d) close()


2. What is the correct syntax of the fopen() function?

(a) fopen("filename", "mode");

(b) fopen("mode", "filename");

(c) fopen("filename", "access");

(d) fopen("access", "filename");


3. Which mode is used to open a file for reading in C programming language?

(a) r

(b) w

(c) a

(d) x


4. Which mode is used to open a file for writing in C programming language?

(a) r

(b) w

(c) a

(d) x


5. What is the syntax of the fclose() function?

(a) fclose();

(b) fclose(file);

(c) fclose("filename");

(d) fclose(filename);


6. Find the purpose of the fseek() function in C File Management.

(a) To move the file pointer to a specific position in the file

(b) To close a file

(c) To read a file

(d) To write to a file


7. What is the purpose of the remove() function in C File Management?

(a) To rename a file

(b) To delete a file

(c) To create a new file

(d) To close a file


8. What is the syntax for opening a file in C programming?

(a) FILE *fp = fopen("filename", "mode");

(b) file fp = fopen("filename", "mode");

(c) FILE *fp = fopen("mode", "filename");

(d) none of these

< Check your ANSWER Here >


File Management MCQs with Answers

C Programming File Management Important MCQs with Answers

1. Which function is used to open a file in C programming language?

(a) fopen()

(b) open()

(c) read()

(d) close()

Answer: (a) fopen()


2. What is the correct syntax of the fopen() function?

(a) fopen("filename", "mode");

(b) fopen("mode", "filename");

(c) fopen("filename", "access");

(d) fopen("access", "filename");

Answer: (a) fopen("filename", "mode");


3. Which mode is used to open a file for reading in C programming language?

(a) r

(b) w

(c) a

(d) x

Answer: (a) r


4. Which mode is used to open a file for writing in C programming language?

(a) r

(b) w

(c) a

(d) x

Answer: (b) w


5. What is the syntax of the fclose() function?

(a) fclose();

(b) fclose(file);

(c) fclose("filename");

(d) fclose(filename);

Answer: (b) fclose(file);


6. Find the purpose of the fseek() function in C File Management.

(a) To move the file pointer to a specific position in the file

(b) To close a file

(c) To read a file

(d) To write to a file

Answer: (a) To move the file pointer to a specific position in the file


7. What is the purpose of the remove() function in C File Management?

(a) To rename a file

(b) To delete a file

(c) To create a new file

(d) To close a file

Answer: (b) To delete a file


8. What is the syntax for opening a file in C programming?

(a) FILE *fp = fopen("filename", "mode");

(b) file fp = fopen("filename", "mode");

(c) FILE *fp = fopen("mode", "filename");

(d) none of these

Answer: (a) FILE *fp = fopen("filename", "mode");



Wednesday, February 15, 2023

Comparison between Python, C and Java

This article provides comparison between three most popular computer programming languages: Python, C and Java.

Python, C and Java


Python, C, and Java are three different programming languages. Each language has its own strengths and weaknesses. Following is a short comparison of these three computer programming languages:

Python


Python is a high-level, interpreted language that is known for its simplicity, readability, and versatility. It has a vast standard library, and it is often used for scripting, web development, data analysis, machine learning, and artificial intelligence. It is dynamically typed, which means that variables don't need to be explicitly declared, and it has a simple syntax that makes it easy to learn and use.

  • Pros: Easy to learn, concise and readable syntax, large standard library, dynamic typing, strong support for scientific computing and data analysis, good for scripting and automation tasks.
  • Cons: Slower compared to other compiled languages, not the best option for low-level programming, doesn't offer as much control over memory management, and not ideal for developing large-scale applications.

C Language

C is a low-level language that is used for system programming, embedded systems, and other tasks that require direct access to hardware. It is a compiled language, which means that it is translated into machine code by a compiler before being executed. C is known for its efficiency, control over memory management, and low-level access to system resources.

  • Pros: Fast and efficient, direct access to system resources, control over memory management, widely used in system programming and embedded systems.
  • Cons: Complex syntax, difficult to learn, no built-in support for object-oriented programming, no automatic garbage collection, prone to memory leaks and buffer overflows.

Java


Java is a high-level language that is known for its portability, security, and object-oriented programming features. It is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM). Java is used for a wide range of applications, including web development, mobile app development, and enterprise software.

  • Pros: Platform independence, strong support for object-oriented programming, automatic memory management, rich standard library, good for developing large-scale applications.
  • Cons: Slower compared to other compiled languages, requires the JVM to be installed on the system, more verbose syntax than Python, and requires more memory compared to other languages.

Summary:

In summary, each language has its own strengths and weaknesses, and the choice of language depends on the specific requirements of the project. Python is often used for scripting and data analysis, C is used for system programming and embedded systems, and Java is used for enterprise software and mobile app development.


Saturday, December 31, 2022

C program to print Memory Address of a variable

In C program, we use variables to store some value. Value of any variables are stored in a memory.

We can print memory address using following two methods:
  • By using address of (&) operator
  • By using pointer variable
 "%p" is a format specifier to print a memory address in C language.

Let's see a program to print a memory address of a variable.


#include<stdio.h>

int main(){
	int number=5;
	float percen = 80.5;
	
	printf("Memory address of number variable: %p \n", &number);
	printf("Memory address of percen variable: %p", &percen);
	
	return 0;
}

Output of Program

Memory address of number variable: 000000051FE1C
Memory address of percen variable: 000000051FE18

Note:

You will get different memory address as output in your program execution.
At every run your output may change.





Tuesday, June 28, 2022

Palindrome numbers between give range

This program will print list of palindrome numbers between given range. 

// C Program to Generate palindrome numbers in a given range of numbers.
#include<stdio.h>

int main(){
   int i, start, end, rem, rev_num, temp;

   printf("Enter first number (lower limit): ");
   scanf("%d",&start);

   printf("Enter last number (upper limit): ");
   scanf("%d",&end);

   printf("List of Palindrome numbers between %d - %d: ",start, end);
   
   for(i=start; i<=end; i++)
   {
      temp = i;
      rev_num=0;
      
      while(temp){
         rem = temp%10;
         temp = temp/10;
         rev_num = rev_num*10 + rem;
      }
      if(i == rev_num)
         printf("%d ", i);
   }
   return 0;
}

Output of program
Enter first number (lower limit): 80
Enter last number (upper limit): 150
List of Palindrome numbers between 70 - 200: 88 99 101 111 121 131 141


Tuesday, May 31, 2022

Flowchart to print Series of Numbers divisible by 3

Draw a flowchart to print Series of Numbers divisible by 3:

2178  726  242  81  27  9  3  1 

Logic: 

  • Step 1: Read a starting number which is divisible by 3
  • Step 2: Divide current number by 3 to get a next number
  • Step 3: Repeat Step 2 until you get a number 1. 
Following flowchart is prepared using a Raptor Tool.

Flowchart to print series of numbers divisible by 3
Figure: Flowchart to print series of numbers divisible by 3


This Raptor flowchart also uses a CEILING function. In Raptor, CEILING returns the lowest integer value greater than or equal to the provided argument. 

For example, 

  • CEILING(15.8) is 16 
  • CEILING(3.2) is 4 
  • CEILING(- 4.2) is - 4

You may like to visit following top 10 flowcharts.