Skip to main content

Day 11: Statistics, Sorting, and Searching

Sorting

Selection sort

Homework

Cipher
Code in action


Code for Cipher.h

#ifndef _CIPHERH_
#define _CIPHERH_

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

char *getInput(void)
{
  int mem=16; 
  char *message=malloc(mem); //Preallocate memory
  printf("Input Message\n");
fgets(message,mem,stdin); //Take user input 

while(message[strlen(message)-1]!='\n') //Check if there's still space
{
mem*=2; 
message=realloc(message,mem); //Double amount of space
fgets(message + mem/2 - 1, mem/2 + 1, stdin); //Read the rest of the line
}
return message; //return character array message
}

int EncryptMessage(void)
{
  char *message=getInput(); //call getInput function
  int ch; //initialize variable ch
  
  int shift; //initialize variable shift
  
  printf("\nInput number of characters to shift: \n");
  scanf("%i",&shift); //ask user to choose shift for encryption
  
  char newarray[strlen(message)]; //initialize array that is the same length as the message to be encrypted
  
  for(int k=0; k<=strlen(message); k++)
  {
    ch=*(message+k); //set ch equal to the current value in the array
    if(ch>='a' && ch<='z')
    {
      ch=ch+shift; //encrypt letter
      if(ch>'z')
      {
        ch=ch-26; //if the value goes past z, subtract 26
      }
      if(ch<'a')
      {
        ch=ch+26; //if value goes past a, add 26;
      }
      newarray[k]=ch; //write new value into array
    }
    
    if(ch>='A' && ch<='Z')
    {
      ch=ch+shift;
      if(ch>'Z')
      {
        ch=ch-26;
      }
      if(ch<'A')
      {
        ch=ch+26;
      }
      newarray[k]=ch;
    }
    
    else
    {
      newarray[k]=ch; //ignore non-letters
      continue; //continue
    }
  }
  printf("Encrypted Message: \n %s\n",newarray); //print encrypted message
}

#endif

Code for main.c

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

int main()
{
  EncryptMessage(); //Call message encryption function
  return 0;

}

Comments

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...