fork download
  1. #include <stdio.h>
  2. void ignore_string(char*& s, char*& p, char c);
  3. char* remove_comments(char* s);
  4.  
  5. int main(void){
  6. char s[] =
  7. R"xXx(
  8. int main() {
  9. //int\
  10. int x;
  11. }
  12.  
  13.  
  14. )xXx";
  15. puts( remove_comments(s) );
  16. return 0;
  17. }
  18.  
  19.  
  20. //удаление комментариев за один проход с затиранием
  21. char* remove_comments(char* s){
  22. char* i, *t = s;
  23. for(char* p = s; *s; *s = *p){
  24. if((*p == '"') || (*p == '\''))
  25. ignore_string(s, p, *p);
  26. else if((*p == '/') && (*(p + 1) == '/')){ //однострочные комментария
  27. i = p + 2;
  28. while(*i && (*i != '\r') && (*i != '\n'))
  29. ++i;
  30. p = i;
  31. } else if((*p == '/') && (*(p + 1) == '*')){ //многострочные комментарии
  32. i = p + 2;
  33. while(*i){
  34. if((*i == '*') && (*(i + 1) == '/')){
  35. i += 2;
  36. break;
  37. }
  38. ++i;
  39. }
  40. p = i - 1;
  41. *p = ' ';
  42. } else
  43. ++s, ++p;
  44. }
  45. return t;
  46. }
  47.  
  48. //пропуск
  49. void ignore_string(char*& s, char*& p, char c){
  50. char* i = p + 1;
  51. while(*i && (*i != c)){
  52. if(*i == '\\'){
  53. if(! *++i)
  54. break;
  55. }
  56. ++i;
  57. }
  58. if(*i == c)
  59. ++i;
  60.  
  61. while(p != i)
  62. *s++ = *p++;
  63. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
		int main() {
			
			int x;
		}