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

This is in C: -- Q1.1 : Expain the 3 outputs observed using the structure of the

ID: 3845430 • Letter: T

Question

This is in C:

-- Q1.1 : Expain the 3 outputs observed using the structure of the code and why this code is poor.

-- Q1.2 With few changes you can improve the code. You need to replace gets() and strcmp() with the similar ones bur more safe. Do a google search, find them and rewrite the code again.Send back the new link.

*/

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

int main(void)
{
char buffer[4];
int pass_flag = 0;

printf(" Enter the password : ");
gets(buffer);   

if(strcmp(buffer, "pswd"))   
{
printf (" Wrong Password ");
}
else
{
printf (" Correct Password ");
pass_flag = 1;
}

if(pass_flag)
{
/* Now Give root or admin rights to user*/
printf (" Root privileges given to the user ");
}

return 0;
}

Explanation / Answer

1)

The code is not making proper use of strcmp. code wshould not give root privledges
if pswd is entered.
output 1:
Enter the password :
pswd

Correct Password

Root privileges given to the user

output 2:
Enter the password :
psswd

Wrong Password


The code is poor because of the warning: the `gets' function is dangerous and should not be used.
gets should be avoided in code.
instead scanf can be used

2)
This can be rectified by using scanf and modifying the
strcmp function in the code

updated code:#include <stdio.h>
#include <string.h>
int main(void)
{
char buffer[4];
int pass_flag = 0;
printf(" Enter the password : ");
scanf("%s",buffer);
if(strcmp(buffer, "pswd") != 0)   
{
printf (" Wrong Password ");
}
else
{
printf (" Correct Password ");
pass_flag = 1;
}
if(pass_flag == 1)
{
/* Now Give root or admin rights to user*/
printf (" Root privileges given to the user ");
}
return 0;
}

output:
Enter the password :
pswd

Correct Password

Root privileges given to the user

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote