Answered! With few changes you can improve the code. You need to replace gets() and strcmp() with the similar ones bur more safe….

With few changes you can improve the code. You need to replace gets() and strcmp() with the similar ones bur more safe.

I need help with the strcmp portion.

*/

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

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

printf(“n Enter the password : n”);
gets(buffer);

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

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

return 0;
}

Expert Answer

 #include <stdio.h>

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

OUTPUT:

Enter the password :
pswd

Correct Password

Root privileges given to the user

Note:
strncmp you can limit the search, so that it doesn’t reach non-accessible memory.

Still stressed from student homework?
Get quality assistance from academic writers!