0% found this document useful (0 votes)
21 views3 pages

C and Java String Manipulation Programs

Uploaded by

cse242004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

C and Java String Manipulation Programs

Uploaded by

cse242004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Week 1.

Cns lab program

Write a C program that contains a string(charpointer) witha value\Hello World’.The


program should XOR each
character in this string with 0 and display the result.
PROGRAM:
#include<stdio.h>

int main()
{
char str[]="Hello World";
char str1[11];
int i,len;
len=strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i]^0;
printf("%c",str1[i]);
}
printf("\n");
return 0;
}
Output: Hello World

2. Write a C program that contains a string (char pointer) with a value \Hello
World’. The program should AND or and XOR each character in this string with 127
and display the result.

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

char str[]="Hello World";


char str1[11];
char str2[11];
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
{
str[i]=str[i]&127;
printf("%c",str1[i]);
}
printf("\n");
for(i=0;i<len;i++)
{
str2[i]= str[i]^127;
printf("%c",str2[i]);
}
printf("\n");
return 0;

Week 3

Write a Javaprogramtoperformencryptionanddecryption usingthe following algorithms:


a) Ceaser Cipher
b) Substitution Cipher
c) Hill Cipher

A) Ceaser cipher

import [Link].*;
import [Link];

public class ceasercipher


{

static Scanner sc = new Scanner([Link]);


static BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));

public static void main(String[] args) throws IOException


{

[Link]("Enter any String: ");


String str = [Link]();

[Link]("\nEnter the Key: ");


int key = [Link]();

String encrypted = encrypt(str, key);


[Link]("\nEncrypted String is: " + encrypted);

String decrypted = decrypt(encrypted, key);


[Link]("\nDecrypted String is: " + decrypted);
[Link]();
}

public static String encrypt(String str, int key) {


String encrypted = "";

for (int i = 0; i < [Link](); i++) {


int c = [Link](i);

if ([Link](c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
} else if ([Link](c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}

encrypted += (char) c;
}

return encrypted;
}

public static String decrypt(String str, int key) {


String decrypted = "";

for (int i = 0; i < [Link](); i++) {


int c = [Link](i);
if ([Link](c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
} else if ([Link](c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}

decrypted += (char) c;
}

return decrypted;
}
}

You might also like