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

Need help knowing exactly what values the for loops and if/else statements are c

ID: 3884790 • Letter: N

Question

Need help knowing exactly what values the for loops and if/else statements are checking against...if you can clearly explain the steps the code is taking it would be very appreciated...

void part5(char buffer[], const int bufferSize){
const char allowed[] = "a3hUD6Cc8bTzv";
int i, j, x;

for(i=0; i<bufferSize; ++i){
char c;

x = getchar();
if(x == EOF)
explode(10);
else if(x == ' ')
explode(11);

c = (char)x;
for(j=2; j<10; ++j){
if(c == allowed[j]){
buffer[i] = c + 1;
break;
}
}
if(j == 10){
explode(12);
}

}

j = 0;
for(i=1; i<bufferSize; ++i){
if(buffer[i] != buffer[i-1]){
j = 1;
break;
}
}
if(j == 0)
explode(13);


/* Windows encodes an end-of-line as two characters: */
/* Linux just uses */
/* This will accept either version */
x = getchar();
if(x == ' ')
x = getchar();
if(x != ' ')
explode(14);
}

Explanation / Answer

Code with comments:

void part5(char buffer[], const int bufferSize){
const char allowed[] = "a3hUD6Cc8bTzv";
int i, j, x;
/* This for loop will run up to length or size of the character buffer.*/
for(i=0; i<bufferSize; ++i){
char c;
x = getchar();
/* This is the condition where we have two scenarios : */
/* 1)end of file getc() returns EOF. 2) if getc() fails*/
if(x == EOF)
/* Here we will call explode function with 10*/
explode(10);
/* This is else if condition to check to next element in the getchar()*/
else if(x == ' ')
explode(11);
c = (char)x;
/* This for loop will get the allowed char array filed from 3rd position and */
/* it will use to check the char comparison with entered char and allowed char array.*/
for(j=2; j<10; ++j){
/* This is the condition where we have the comparison of entered with and allowed array.*/
if(c == allowed[j]){
buffer[i] = c + 1;
break;
}
}
/* after comparison char, we will call the function explode with integer 12.*/
if(j == 10){
explode(12);
}
}
j = 0;
/*This is again the buffer loop for the char comparison.*/
for(i=1; i<bufferSize; ++i){
/* Here is the actual buffer comparison will happen after the process of allowed array.*/
if(buffer[i] != buffer[i-1]){
j = 1;
break;
}
}
/* if there is no comparison success, then we will call explode with integer 13.*/
if(j == 0)
explode(13);

/* Windows encodes an end-of-line as two characters: */
/* Linux just uses */
/* This will accept either version */
x = getchar();
if(x == ' ')
x = getchar();
if(x != ' ')
explode(14);
}

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