Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CISC 130 Lab #5 Introduction You will work with a randomly assigned partner part

ID: 3721938 • Letter: C

Question

CISC 130 Lab #5 Introduction You will work with a randomly assigned partner partner assignments will be posted to Canvas. Only one of you should submit the assignment. In this lab, you will be practicing problem-solving, assignment, input, output, mathematical operations, if statements, loops, arrays, and strings in C. The Lab Setup The following steps will help you set up the lab. For further reference, you may want to look at Lab 1 1. Create a new folder in your OneDrive CISC130 folder, call it Lab5. 2. In CodeBlocks, make a new c file. Title it namel_name2_lab5.c and save it in the folder you created above. Where your names take the place of nameX above. 3. Copy the code below into your lab5 file. YOUR NAMES HERE #include int main () printf("PART 11n ") printf("InInPART 2 In") SOLUTION FOR PART 1 below*** *SOLUTION FOR PART 2 below*** 4. Make sure your names replace the contents of the comment that says /YOUR NAMES HERE*/ Activities All of the following activities are to be completed in a single file. Remember to include comments in your code to indicate your intentions and don't forget proper indentation. Please note, as you finish each part the results will continue to print as you work on, and test, the next part. This is fine. Part 1: Encryption

Explanation / Answer

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

int main(){
//PART 1: ENCRYPTION GOES HERE************************************************

printf("Please enter your message: ");
char str[1001] = "";
int size = 0;
int ans = 0;

fgets(str, 1000, stdin);
size = strlen(str);
str[size - 1] = '';

printf("Would you like to (1) encrypt this message or (2) decrypt it: ");
scanf("%d", &ans);

int i = 0;
int cas = 0;

if(ans == 1){//Encrypt it
for(i = 0; i < size; i++){
if(str[i] >= 'a' && str[i] <= 'z')//it's small letter
cas = 0;
else if(str[i] >= 'A' && str[i] <= 'Z')//It' capital
cas = 1;
else//Some symbol we don't care
cas = -1;

if(cas != -1)
str[i] = (str[i] + i + 1);

if(cas == 0)
str[i] = (str[i] - 'a')%26 + 'a';//If it goes beyond z we have to get it back

if(cas == 1)
str[i] = (str[i] - 'A')%26 + 'A';//If it goes beyond z we have to get it back
}
printf("Your encrypted message is: %s ", str);

}else if(ans == 2){//Decrypt it
for(i = 0; i < size; i++){
if(str[i] >= 'a' && str[i] <= 'z')//it's small letter
cas = 0;
else if(str[i] >= 'A' && str[i] <= 'Z')//It' capital
cas = 1;
else//Some symbol we don't care
cas = -1;

if(cas != -1)
str[i] = (str[i] - i - 1);

if(cas == 0)
str[i] = (str[i] + 'a')%26 + 'a';//If it goes beyond z we have to get it back

if(cas == 1)
str[i] = (str[i] + 'A')%26 + 'A';//If it goes beyond z we have to get it back
}
printf("Your decrypted message is: %s ", str);

}else{//Weird Input, get it out of here
printf("Weird Input, Quitting Program");
return -1;
}

//PART 2:NUMBER TO STRING GOES HERE************************************************

//To complete the challenge use a dynamically allocated pointer
//char *str; - For dynamic allocation

int num = 0;
printf(" Enter the number to be converted to string: ");
scanf("%d", &num);

printf("Enter the size of string: ");
scanf("%d", &i);

str[i] = '';//Here the n umber will be added backward so that we don't have to reverse it, at the end we can add the 0's required
i--;

//*str = (char*) malloc(i * sizeof(char));; - For dynamic allocation

while(num != 0){
str[i] = num%10 + '0';//Put the last digit in the string
i--;//Go to the next place
num /= 10;//Divide by 10 to get the next place, i.e from ones to tens or from tens to hundreds
}

while(i >= 0){
str[i--] = '0';
}
printf("The string is - %s", str);
//free(str); - for dynamic allocation
return 0;
}

//Hope this helps