Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Basics
Home > Programs > Basics

93. Write code to remove comment lines.

Answer:

#include <iostream>
using namespace std;
string removeComments(string prgm)
{
    int n = prgm.length();
    string res;
    /* Flags to indicate that single line and multpile line comments */
    /* have started or not */
    bool s_cmt = false;
    bool m_cmt = false;
    /* Traverse the given program */
    for (int i=0; i<n; i++)
    {
        /* If single line comment flag is on, then check for end of it*/
        if (s_cmt == true && prgm[i] == '\n')
            s_cmt = false;
        /* If multiple line comment is on, then check for end of it*/
        else if (m_cmt == true && prgm[i] == '*' && prgm[i+1] == '/')
            m_cmt = false, i++;
        /* If this character is in a comment, ignore it*/
        else if (s_cmt || m_cmt)
            continue;
        /* Check for beginning of comments and set the approproate flags*/
        else if (prgm[i] == '/' && prgm[i+1] == '/')
            s_cmt = true, i++;
        else if (prgm[i] == '/' && prgm[i+1] == '*')
            m_cmt = true, i++;

        /* If current character is a non-comment character, append it to res*/
        else res += prgm[i];
    }
    return res;
}

/* Driver program to test above functions*/
int main()
{
    string prgm = " /* Test program */ \n"
                " int main() \n"
                " {         \n"
                "     // variable declaration \n"
                "     int a, b, c; \n"
                "     /* This is a test \n"
                "         multiline     \n"
                "         comment for \n"
                "         testing */     \n"
                "     a = b + c;     \n"
                " }         \n";
    cout << "Given Program \n";
    cout << prgm << endl;
    cout << " Modified Program ";
    cout << removeComments(prgm);
    return 0;
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: