Skip to content

Instantly share code, notes, and snippets.

@ikbalsingh
Created April 26, 2019 17:06
Show Gist options
  • Save ikbalsingh/30c4118ba35c53c80f488ef8ad3a5293 to your computer and use it in GitHub Desktop.
Save ikbalsingh/30c4118ba35c53c80f488ef8ad3a5293 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class C_Comment_Parser {
public static void main(String[] args) {
System.out.println(parseComments("#include <stdio.h>\nint main()\n{\n\t/* printf() displays \n\t\t the string inside quotation*/\n\tprintf(\"Hello, World!\");\n\treturn 0;\n}"
));
}
public static String parseComments(String code){
char arr[] = code.toCharArray();
StringBuilder sb = new StringBuilder();
for(int i = 0; i<arr.length; i++){
// System.out.println(arr[i]);
if(arr[i] == '/' && i+1<arr.length && arr[i+1]=='/'){
int j;
for(j=i+2; j<arr.length; j++){
if(arr[j] =='\n'){
break;
}
}
if(j != arr.length)
sb.append(arr[j]);
i = j;
}else if(arr[i] == '/' && i+1<arr.length && arr[i+1]=='*'){
int j;
for(j=i+2; j<arr.length; j++){
if(arr[j]=='*' && j+1 < arr.length && arr[j+1] == '/')
break;
}
i = j+1;
}else{
sb.append(arr[i]);
}
}
return sb.toString();
}
}
/*
Output :
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment