String Arrays

Discussion in 'C#' started by nagarjuna02, Feb 15, 2010.

  1. nagarjuna02

    nagarjuna02 New Member

    Joined:
    Feb 15, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi
    I am in a process of designing relational calculator.
    If a user supplied a string like -3.33+44*456/2.2-3+4....
    I want to store it in string array as
    -3.33
    +44
    *
    456
    /
    2.2
    -3
    +4
    ......
    (that is *, /, +ve value, -ve value separately and in serial order into an string array)

    This is the code I wrote:
    string a = "-3.33+44*456/2.2-3"
    string[] ip = new string[25];
    int k = 0;


    for (int i = 0; i < a.Length; i++)
    {
    if (a.Substring(i, 1) == "+" || a.Substring(i, 1) == "-" || a.Substring(i, 1) == "*" || a.Substring(i, 1) == "/" || a.Substring(i, 1) == "^")
    {
    for (int j = i + 1; j < a.Length; j++)
    {
    if (a.Substring(j, 1) == "+" || a.Substring(j, 1) == "-" || a.Substring(j, 1) == "*" || a.Substring(j, 1) == "/" || a.Substring(j, 1) == "^")
    {
    if (a.Substring(i, 1) == "+" || a.Substring(i, 1) == "-")
    {
    ip[k] = a.Substring(i, j-i);
    k++;
    }
    else
    {
    ip[k] = a.Substring(i, 1);
    k++;
    ip[k] = a.Substring(i + 1, (j -i)-1);
    k++;
    }
    i = j;
    break;
    }
    }
    }

    }
    But its not working properly:
    Its storing only one element in the array.
    From last two days I am braking my head.
    Please help me.
    Thank You.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice