views:

93

answers:

2

We are taking input from a text file.

Every line is split in strings at white spaces, so we can further classify the elements. Making the string the problem is, that I also want to read string literals (e.g. "Thank you") as they are without splitting and comments too (both // and /* ....*/). Is there any way I can do this?

A: 

My sample text is:

using system.text.RegularExpressions;
namespace check_regex               //Using regex.
{
    class Program
    {
        static void Main(string[] args)
        {
            //This is a program.

            Console.WriteLine("this is a string");
            /* okay okay
              blah blah blah
              sumthng blah 908657580@#%$ */

    }
}

The text input will be something like this.

intrinsic
A: 

The standard way to do this is to use a state machine - reading each character in sequence, switch to and from a 'string' state when you see a ", and switch to and from a 'comment' state when you see //, /* or */. In each state you can append the characters you read to a stringbuilder, and that way find each type of string in your input

thecoop