#include <stdio.h>
int main() {
char c;
_Bool insideword = 0;
do {
scanf("%c", &c);
int counter = 0;
if (c != ' ' || c != '.') {
insideword = 1;
}
if (c == ' ') {
insideword = 0;
}
if (insideword && c != ' ') {
printf("%c",c);
}else{
if(insideword ==0 && c==' '){
counter++;
}
while(counter>=1){
printf(" %c", c);
}
}
}while (c!= '.');
return 0;
}
This code reads characters until a period and tries to print words separated by spaces, but it has several logical errors. The variable `counter` is reset to 0 each loop iteration, and the condition `c != ' ' || c != '.'` is always true, so `insideword` is always set to 1. The while loop at the bottom will run forever if `counter` is 1 or more.
- The condition `c != ' ' || c != '.'` is always true because any character is either not a space or not a period. Think about what logical operator you need to check if a character is neither a space nor a period.
- The variable `counter` is declared inside the loop and reset each time, so it never accumulates. Consider where you should declare and update a counter to track the number of spaces between words.