Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please correct this C coding program. Their are mistakes in the code and I need them fixed. The direction to the coding program are below

Please correct this C coding program. Their are mistakes in the code and I need them fixed. The direction to the coding program are below the source code of the prgram. One of the main issues is that the code gets stuck in a never ending loop when changing the shift value.

Source code:

#include

#include

#include

int getShift( void )

{

int i;

printf ("Enter new shift value: ");

scanf ("%d", &i);

printf (" ");

return i;

}

void banner (void)

{

printf ("------------------------------- ");

printf ("| 1) Change Shift (default 3) | ");

printf ("| 2) Encrypt a message | ");

printf ("| 3) Decrypt a message | ");

printf ("| 4) Quit | ");

printf ("------------------------------- ");

fflush(stdin);

}

int getUserChoice (void)

{

int i;

scanf ("%d", &i);

return i;

}

void getString (char buf[])

{

char t;

printf ("Input: ");

scanf ("%c", &t);

scanf ("%[^ ]s", buf);

return;

}

void encrypt (char buf[], int shift)

{

int len = 0, i;

len = strlen (buf);

for (i=0; i

{

buf[i] = buf[i] + shift;

}

printf ("Output: %s ", buf);

return;

}

void decrypt (char buf[], int shift)

{

int len = 0, i;

len = strlen (buf);

for (i=0; i

{

buf[i] = buf[i] - shift;

}

printf ("Output: %s ", buf);

return;

}

#define MAX_BUFF 500 /*Change your input buffer as per your requirement*/

int main()

{

int option;

char buf[MAX_BUFF] = {'\0'};

int shift = 3; /*Default Shift 3*/

while (1)

{

banner ();

printf ("Option: ");

option = getUserChoice();

memset (buf, '\0', MAX_BUFF);

switch (option)

{

case 1:

shift = getShift();

continue;

case 2:

printf ("In 2 .. ");

getString(buf);

encrypt (buf, shift);

continue;

case 3:

printf ("In 3 .. ");

getString(buf);

decrypt (buf, shift);

continue;

case 4:

return 0;

default:

printf ("Invalid Choice, try again ");

continue;

}

}

return 0;

}

image text in transcribed

image text in transcribed

Requirements: Use the following: - functions - function returns - function parameters - strings -loops - if statements Include the following functions - int getUserChoice() - int getshift() - void getString(char buf[1) - void encrypt (char buf, int shift) - void decrypt (char bufl, int shift) The parameters of these functions are given, do not change them. However, the body of these functions need to be filled in as does the body of main) getUserChoice() will be used to print the menu and get the users choice of 1, 2, 3, or 4 * Your program should either quit or handle invalid menu options. getstring) will be used to get the string the user wants encrypted * These are the only 3 functions scanf.) or fgets() are allowed to appear in. This means input functions cannot appear in main() Your default shift value should be set to 3 * DO NOT encrypt/decrypt the newline ( ) character or NULL terminator (1) character in your strings

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

describe the main employment rights as stated in the law

Answered: 1 week ago