Please give an in-depth explanation of what this method is doing . For example,
ID: 3887518 • Letter: P
Question
Please give an in-depth explanation of what this method is doing. For example, what are the values of the arrays of buf1[] and buf2[], if buf2 = "hello" when the readInput() method is called. Note, readInput(buffer, bufferSize) simply returns the array of charcters contained and breaks when end-of-line is reached...that is the end-of-line characters are not included in the return of the 'buffer' input. I would also like to know the index of 'i' that is used for the first execution of the for loop...More exactly, what is the value of 'i' for the first execution of the statements buf1[i] and buf2[bufSize - 1 - i]... Also, please do not just answer the examples I have included in the above paragraph. I have a good idea already of what the method does, but an in-depth analysis would help me to understand it better.
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
Hi,
Firstly, the definition of explode is not given, so i am not really sure what goes on inside the explode function,
now coming to the main function,
Given an array of characters with its size,
we are reading a new array with same size and checking if inputted array is reverse of the given array,
ex: if buf1 was hello,
then the if wont fail if
buf2 is "olleh"
so basically inside the loop,
buf1[i] stands for the ith character
and buf2[bufsize-1-i] stands for the ith character from last
so first say for the example you asked ''hello""
if buf2 was entered "olleh"
then
iteration1 - i=0 , buf1[0]==buf2[5-1-0] h==h true
iteration2 - i=1 , buf1[1]==buf2[5-1-1] e==e true
iteration3 - i=2 , buf1[2]==buf2[5-1-2] l==l true
iteration4 - i=3 , buf1[3]==buf2[5-1-3] l==l true
iteration5 - i=4 , buf1[4]==buf2[5-1-4] o==o true
so we are comparing every ith character to ith character from last in buf2
so if buf2 is reverse of buf1 then explode function wont get called,
if not it will get called.
Thumbs up if this was hepful, otherwise let me know in comments.