I am writing a program that takes a user's comment. Specifically one that has input outside of /*
and */
and also inside. I have written my loop to find the char "/"
in my array and I am unsure how to remove it and everything in between it until it appears again. For example if my input was "comment /* this is my comment */"
I need to remove the /*
*/
and contents between. So my output would just be "comment"
. If there is no "/* and */"
it doesn't remove anything. I know I need a loop but how would I write a loop that removes chars in the array until the next "/"
appears and removes it as well?
My code is as follow:
#include <stdio.h>
#include <string.h>
void remove_comment(char *s1, char *s2){
for(; *s1 != '\0'; s1++){ //loops through array until null value
if(*s1 == '/'){ //if array has '/' stored
//clear array elements till next '/' and removes it as well
}
else{
return; //do nothing to array
}
strcpy(s2,s1); //copies new modified string to s2 for later use
}
int main(){
char s1[101]; //declares arrays up to 100 in length with room for null character
char s2[101];
printf("Enter a comment: "); //enter a comment
fgets(s1, 100, stdin); // saves comment to array
remove_comment(s1,s2); //calls function
printf("%s", s2); //prints my modified array
return 0;
}