I'm trying to make a string reverse program. I'm trying to fix bugs with over/underflow. I fixed the first one, so now I've set MAX = 8 just to make it easier to debug. It works correctly. But when my input is shorter than MAX, the program breaks down.
So, as you can see, the string "QWERTY ASDFGH" is cut correctly and I have "QWERTY A" (which is exactly 8 characters), then the program reverses it and the output is "A YTREWQ". It does exactly what I want.
But if the actual length is less then 8, I press enter, and nothing happens. I have to press it twice and still have this broken input and output. The output goes to a new line, but it's not how it has to work.
There's my source code:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define MAX 8 // not including '\0' symbol
void printAsterisks(uint8_t);
void clearInputBuffer(void);
int main(void) {
char word[MAX + 1], temp;
int length, index;
printAsterisks(46);
printf("Please, type in any string. To exit give me #.\n");
printAsterisks(46);
while(1) {
index = 0;
printf("Your string : ");
fgets(word, MAX + 1, stdin);
if(word[0] == '#') {
printAsterisks(39);
printf("The program has been cancelled by User.\n");
printAsterisks(39);
break;
}
clearInputBuffer();
length = strlen(word);
for(index = 0; index < length / 2; index++) {
temp = word[index];
word[index] = word[length - index - 1];
word[length - index - 1] = temp;
}
printf("Reversed : %s\n", word);
}
return 0;
}
void printAsterisks(uint8_t count) {
for(uint8_t i = 0; i < count; i++) {
printf("*");
}
printf("\n");
}
void clearInputBuffer(void) {
static char buff;
while((buff = getchar()) != '\n' && buff != EOF);
}