在线编译器 C

#include <stdio.h> #include <string.h> int main() { char str[201]; fgets(str,sizeof(str) ,stdin); str[strcspn(str,"\n")]='\0'; for(int i=0;str[i]!='\0';i++){ if(str[i]==' '){ str[i]='_'; } }printf("%s\n",str) return 0; }
This code reads a line of text from the user, replaces every space character with an underscore, and prints the result. It removes the trailing newline from the input before processing.

- There is a missing semicolon on the line that prints the result. Check the line with `printf` and add the needed punctuation.
- The loop condition uses `str[i]!='\0'` which is correct, but ensure the loop does not go beyond the string length. Since you already removed the newline, this is fine, but think about what happens if the input is longer than 200 characters.