//So in my understanding of this method takes buff1[] array and buffersize as in
ID: 3887449 • Letter: #
Question
//So in my understanding of this method takes buff1[] array and buffersize as input and it calls readInput() method by sending the buff array and bufferSize. The for loop executes up to bufSize and has a pre incremented i. It then if (buf1[i] != buf2[bufSize - 1 -i] returns true, the bomb explodes. I intiallly thought that any palindrome would work, but that was not correct. Is the first for loop execution at i = 1? Also, I know that in C programming that the compiler signifies the end of char array with the ' ' character...is this taken into account in the loop? (I will display the readInput() method below part6() method for clarity. Also, if any of what I think I know is incorrect, knowing that would be helpful. Basically I need an extensive run through of EXACTLY what the code does. This is a hw assignment so I need to fully understand the concepts.
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);
}
--------------------------------------------------------------------- (read input method)
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;
}
}
Explanation / Answer
how does for loop works??
first initialization is executed just once.
then the condition is checked .
if true execute the body of loop and then execute the manipulation statement like incrementing loop variable.
After executing manipulation statement evalute the condition and if it is true repreat the procedure.
if false exit the loop.
so for loop's first execution will start at 0;
I know that in C programming that the compiler signifies the end of char array with the ' ' character...is this taken into account in the loop?
now in readInput() method;
we are using getChar() method which rerturns an int representing character and hence we cast it to (char) in statment buffer[i] = (char)c;
we enter a string and hit enter . This string will be read character by character using getchar() and forloop.
enter is represented by ' ' in windows and in linux by ' '.We dont want the these characters to be part of our buffer array hence we check for them. And the read input accepts number of character=buffsize;
if(buf1[i] != buf2[bufSize - 1 - i])
if buf1 and buf2 are NOT palindromes then it will explode
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.