Wednesday, April 6, 2011

Removal of ";" from statements ;; in C

I need to remove ";" from statements in C which have ";;". Eg:

main()
{
    int i;;
    int j,k;;
    int l;
    for(i=0;i<10;;)
    {}
}

should become:

main()
{
    int i;
    int j,k;
    int l;
    for(i=0;i<10;;)
    {}
}
From stackoverflow
  • Consider using Find & replace command. Any decent text editor has one (Notepad++, KWrite, not to mention Emacs). Search for ;; and replace it with ;.

    If you have plenty of code and ; can happen inside char* values, use regexp :)

    EDIT: Fine, you should search for: ;; *\n and then replace it with ;\n.

    Daniel : NOT to mention emacs? BLASPHEMER
    Abgan : :-D That was intentional :-D Always happy to amuse others :-D
  • UltraEdit could be a good choice..

  • My first answer was wrong because I missed the ;; in the head of the for loop, which does serve a purpose.

    The following perl script will replace all ;; followed by whitespace with a single ;. It will do that for all C files (file extension .c) in the current directory and its subdirectories.

     perl -i -p -e 's/;;(\s*)$/;$1/g' `find | grep .c`
    

    Thanks to Sean Bright for fixing my original mistake. (See the comments.)

    I just want to point out that the extra ; at the end of line shouldn't really present a problem. You could just leave them there, or you could just use your text editor to search and replace the ones that are extraneous.

    strager : The ;; would be replaced in the for loop, which isn't what he wants.
    Simon Knights : But is that what he wants? Its not clear.
    Bill the Lizard : @strager: Thanks, I missed that in the original format of the code.
    Sean Bright : @Bill: with a slight mod your example would work: perl -i -p -e 's/;;\s*$/;/g' `find | grep .c`
    Bill the Lizard : @Sean: Thanks, I modified it even further to preserve the whitespace.
    Sean Bright : @Bill: I considered that, but trailing whitespace is icky :P
    Bill the Lizard : @Sean: I think my solution will work. It just puts the same whitespace back in after replacing ;; with a single ;. I've only tried it with the OP's example, though.
    Sean Bright : @Bill: the * needs to go inside the parens, but otherwise it looks good (I don't feel comfortable editting other people's answers)
    Bill the Lizard : Thanks, that works beautifully.

0 comments:

Post a Comment