Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void remove_cmmnt(char *s) //essentially the two nested if statements are to skip over the text that contains comments
- {
- int i,j;
- for(i=j=0; s[j] ; ) //if s of j has a value the body will execute, then do the test again etc.
- {
- if(s[j]=='/' && s[j+1] && s[j+1]=='/') //if the current character is '/' and the following character is '/', run the for statement.
- for(j+=2; s[j] && s[j++]!='\n'; ) //the place in the array becomes the character after the the '/', then moves along the array
- ; //until it reaches a newline character. j then increments by 1
- else if(s[j]=='/' && s[j+1] && s[j+1]=='*') //if the current character is '/' and the following character is '*', run the for statement.
- for(j+=2; s[j] && s[++j] && (s[j-1]!='*' || s[j]!='/' || !j++); ) //the place in the array becomes the character after the '*'.
- ; //the place in the array is incremented. Then if the current character
- //and the previous character are '*/', increment j by 1
- else
- s[i++]=s[j++]; //copies the value of s of j to s of i, then increments i and j
- }
- s[i]='\0';
- }
- int main()
- {
- char s[]="/*123***/Hello // Cross\n World /* **NachLeCoders";
- remove_cmmnt(s);
- puts(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement