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

// I don\'t necessarily completely understand this code. From my understanding,

ID: 3887365 • Letter: #

Question

// I don't necessarily completely understand this code. From my understanding, the buf2[] array is input by the user, and when being compared to buf1[], it is read in reverse and checks to see if each char is equal at buf1[i]. But what is the value of buf1[]? I tried so many different debugging methods to try to print out the contents of buf1[] and when I do that is gives me a string of a bunch of chars. So I put that in reverse in order to try and excute the code without hitting explode. But that didn't work either...stuck. I am obviously lacking in the complete understanding of the code so any information helps.

void part6(const char buf1[], const int bufSize){
char buf2[bufSize];
int i, c;

readInput(buf2, bufSize);

for(i=0; i<bufSize; ++i){
if(buf1[i] != buf2[bufSize - 1 - i])
explode(-44);
}

}

Explanation / Answer

code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void explode(const int code);

void readInput(char buffer[], const int bufferSize){

int i;

int c;

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

c = getchar();

/* this checks that you are at the end of the line */

/* Windows encodes an end-of-line as two characters: */

/* Linux just uses */

/* This will accept either version */

if(c == ' '){

c = getchar();

}

if(c == ' '){

break;

}

else if(i<bufferSize){

buffer[i] = (char)c;

}

}

}

void part6(const char buf1[], const int bufSize){

char buf2[bufSize];

int i, c;

readInput(buf2, bufSize);

for(i=0; i<bufSize; ++i){

if(buf1[i] != buf2[bufSize - 1 - i])

explode(-44);

}

}

int main(void) {

const int bufSize = 10;

char buffer[bufSize];

part6(buffer, bufSize);

return 0;

}