Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code,
C#
string text = System.IO.File.ReadAllText(@"D:\Compiler\test.txt");
            Console.WriteLine(text);



            char[] array = ar.ToCharArray();
            char[] asa;
            for (int i = 0; i <= array.Length - 1; i++)
            {
                        if (array[i] == ' ')
                        {
                            

                        }


                  else
                  {
                   letter =array[i]; 
                   Console.Write(letter);
                  }

                }





in this code text read character by character. i want to do this if a space will come then word is complete and each word into it's own string in an array. for example:
void main void,,,,,
output:
exampleArray[0] = void
exampleArray[1] = main
exampleArray[2] = void
how to do this please help.............
Posted

Since your input file suggested that you are writing a compiler (likely for a class), splitting the input based on spaces may come back to cause trouble. There are cases where spaces are significant. For example, in string and character constants.
In the long run, you would probably do better to implement a simple state machine to take the input text as an IEnumerable<char> and return a sequence of objects representing the "token". I.e., identifier, integer/char/string/float/double const, various punctuation symbols (single and multiple character...). This is the lexical analysis of your input. Further along in the processing you can differentiate between reserved-words and user-defined ids.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Mar-13 17:17pm    
Of course, a good point, a 5.
—SA
There is an easier way:

C#
string[] parts = ar.Split(' ');


The result is an array of string, split at the spaces.

For the additional requirement if splitting at ( and ) here the proposal:

First, add a "gremlin" at the end, e.g. '@', this should not be one of your watched symbols. After split at spaces you get
[0]=void
[1]=main(void)@
In this case you repeat on every string and split using first '(', iterate over the parts adding them to a new collection and adding string "(" between them. Now you have
[0]=void
[1]=main
[2]=(
[3]=void)@
After that you iterate over all parts splitting at ')', adding them to a new collection and adding string ")" between them.
Now you have
[0]=void
[1]=main
[2]=(
[3]=void
[4]=)
[5]=@
After you are done remove the 'gremlin', either from the end of the last string if it has more than 2 characters, or the whole last string.
Then you get:
[0]=void
[1]=main
[2]=(
[3]=void
[4]=)

I think you get the picture.
 
Share this answer
 
v2
Comments
Arsalaan Ahmed 20-Mar-13 4:29am    
for (int i = 0; i <= array.Length - 1; i++)
{
if (array[i] == ' ')
{
string cas = new string(array);
Console.WriteLine(cas);
}
else
if (array[i] == '(' || array[i] == ')')
{
Console.Write(array[i]);
}
else
{
letter =array[i];
// Console.Write(letter);

}
this is a code in this code...
void main(void)
str[0]=void
str[1]=main
str[2]=(
str[3]=void
str[4]=)
in this situation how to do this,,,,,
Steve44 20-Mar-13 4:42am    
First, add a "gremlin" at the end, e.g. '@', this should not be one of your watched symbols. After split at spaces you get [0]=void [1]=main(void)@ In this case you repeat on every string and split using first '(', iterate over the parts adding them to a new collection and adding string "(" between them. Now you have [0]=void [1]=main [2]=( [3]=void)@ After that you iterate over all parts splitting at ')', adding them to a new collection and adding string ")" between them. Now you have [0]=void [1]=main [2]=( [3]=void [4]=) [5]=@ After you are done remove the 'gremlin', either from the end of the last string if it has more than 2 characters, or the whole last string. I think you get the picture.
Adding to the solution so formatting works.
Arsalaan Ahmed 20-Mar-13 5:00am    
yes steve,,
Arsalaan Ahmed 20-Mar-13 5:02am    
how to do this can u give me code,,
Hello Arsalan,

How about doing this as
C#
string[] words = text.Split(' ');

Regards,
 
Share this answer
 
Hi Ahmed,

As you have started with opening a file so I believe you want to read that filestream character by character and there you want to get all the strings. I am just modifying your code a little bit if it helps you.

C#
string filePath = @"D:\Compiler\test.txt";
FileStream fs = System.IO.File.OpenRead(filePath);

string delimiters = " \t\n,.()[]";
List<string> allTokens = new List<string>();
StringBuilder sb = new StringBuilder();
            
while (fs.CanRead)
{
    char c = Convert.ToChar(fs.ReadByte());
    if (delimiters.IndexOf(c) >= 0) // character is delimiter
    {
        allTokens.Add(sb.ToString());
        sb.Clear();
    }
    else
    {
        sb.Append(c);
        Console.Write(c);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900