Showing posts with label Deerwalk College. Show all posts
Showing posts with label Deerwalk College. Show all posts

Caesar Cipher in C

 #include<stdio.h>

int main(){

char inputtext[]="Deep Raj Bhujel", ciphertext[100], decipheredtext[100];

int i,shift=3;

for(i=0;inputtext[i]!='\0';i++){

if(isupper(inputtext[i]))

ciphertext[i]=(inputtext[i]-65+shift)%26 + 65;

else if(islower(inputtext[i]))

ciphertext[i]=(inputtext[i]-97+shift)%26 + 97;

else

ciphertext[i]=inputtext[i];

}

ciphertext[i]='\0';

printf("The encrypted string is %s\n\n",ciphertext);

for(i=0;ciphertext[i]!='\0';i++){

if(isupper(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-65-shift)%26 + 65;

else if(islower(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-97-shift)%26 + 97;

else

decipheredtext[i]=ciphertext[i];

}

decipheredtext[i]='\0';

printf("The decrypted string is %s",decipheredtext);

return 0;

}

Cryptography Notes - B.Sc. CSIT


Unit 1 - Introduction and Classical Ciphers - Crypto






C Program - String Palindrome or Not

// Write a C program to check whether the given string is palindrome or not.
// Palindrome should be checked by user defined function.


#include<stdio.h>
#include<conio.h>
#include<string.h>

void palindrome(char []);

void main()
{
    char str[20];
    clrscr();
    printf("Enter string: ");
    fgets(str,sizeof(str),stdin);

    palindrome(str);

    // printf("String is: ");
    // puts(str);
    getch();
}

C Program - Ascending and Descending Order

// Write a program to rea n integer numbers in an array dynamically
// and create two functions to sort in ascending and descending order
// and display the result in the main function.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void ascending(int*, int);
void descending(int*, int);

void main()
{
    int *ptr,n,i;
    printf("Enter n: ");
    scanf("%d",&n);
    ptr = (int*)malloc(n*sizeof(int));
    printf("Enter the elements:\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
    }

    ascending(ptr,n);
    printf("Ascending order: \n");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
    }

    descending(ptr,n);
    printf("\nDescending order: \n");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
    }

    getch();
}

void ascending(int *ptr, int n)
{
    int i,temp;
    while(n!=0)
    {
    for(i=0;i<n-1;i++)
    {
        if(*(ptr+i) > *(ptr+i+1))
        {
        temp=*(ptr+i);
        *(ptr+i)=*(ptr+i+1);
        *(ptr+i+1)=temp;
        }
    }
    n--;
    }
}

void descending(int *ptr, int n)
{
    int i,temp;
    while(n!=0)
    {
    for(i=0;i<n-1;i++)
    {
        if(*(ptr+i) < *(ptr+i+1))
        {
        temp=*(ptr+i);
        *(ptr+i)=*(ptr+i+1);
        *(ptr+i+1)=temp;
        }
    }
    n--;
    }
}