need some C++ help on how to split text

Discussion in 'Mac Programming' started by prostuff1, Oct 5, 2006.

  1. prostuff1 macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #1
    Might as well start off with the links first.

    This one is two the assignment i need to do. So if you don't understand the discription please click the link.

    OK, what i need to do is take the text from t1 split it in half (not sure about using odd numbered amount of text but i have it in my code anyway) and then the take the frist half as the new output for t1 and the second half for t2.

    Example:
    Input
    t1 = help
    t2 = blank_for_now
    Output
    t1 = he
    t2 = lp

    Here is the code i have so far:
    Code:
    procedure_body Split (
    	alters Text& t1,
    	consumes Text& t2
        )
    {
        Integer pos = 0;
    
        if(t1.Length () mod 2 == 1)
        {
    	while(pos <= t1.Length()/2)
    	{
    	    if(t1.Length () mod 2 == 1)
    	    {
    		t2.Add(pos,t1[t1.Length ()/2+1]);
    		t1.Remove(t1.Length ()/2+1, t1[t1.Length ()/2+1]);
    		pos++;
    	    }
    	    else
    	    {
    		t2.Add(pos,t1[t1.Length ()/2+pos]);
    		t1.Remove(t1.Length ()/2+pos, t1[t1.Length ()/2+pos]);
    		pos++;
    	    }
    		
    	}
        }
        else
        {
    	while(pos <= t1.Length()/2)
    	{
    	    if(t1.Length () mod 2 ==1)
    	    {
    		t2.Add(pos,t1[t1.Length ()/2+1]);
    		t1.Remove(t1.Length ()/2+1, t1[t1.Length ()/2+1]);
    		pos++;
    	    }
    	    else
    	    {
    		t2.Add(pos,t1[t1.Length ()/2+pos]);
    		t1.Remove(t1.Length ()/2+pos, t1[t1.Length ()/2+pos]);
    		pos++;
    	    }
    		
    	}
        }
    }
    If anyone can help me with this it would be a big help. I am almost there but seem to be stuck right now.

    Help is appriciated, thanks.
     
  2. Heath macrumors regular

    Joined:
    Aug 19, 2005
    Location:
    Canada
    #2
    A few more gotchas

    You might want to look at handling a couple of the more annoying cases, that may give you insight on how to complete the rest.

    What about an empty string? How would you 'split' an empty string?

    You mentioned a string with a length that is an odd number, but what about the string with length 1, how would you 'split' that in half?
     
  3. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3
    This is a case where C++ just makes it harder. In pure ANSI C this is triveal. You simply compute the address of the mid-point of the string and copy the sub-string to where it goes.

    char t1[] = "foobar";
    char t2[8]; = "";

    if(t1[0]){
    int i;
    i = 1 + strlen(t1)/2;
    strcpy(t2, &(t1));
    t1= '\0';
    }
     
  4. prostuff1 thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #4
    Well, I finally figured it out!!

    "In case anyone was wondering why the code i have does nto look like "normal" C++ it is because OSU (the school i go to) is making us learn a "version" of C++ that they tweaked form regular C++.

    Anyway, here is the final code that i have:
    Code:
    procedure_body Split (
    	alters Text& t1,
    	preserves Integer pos,
    	consumes Text& t2
        )
    {
        Integer count = 0;
    
        while(t1.Length () != pos)
        {
    	if(t1.Length () mod 2 == 1)
    	{
    	    t2.Add(count, t1[pos]);
    	    t1.Remove(pos, t1[pos]);
    	}
    	else
    	{
    	   t2.Add(count, t1[pos]);
    	   t1.Remove(pos, t1[pos]);
    	}
    	count++;
        }
    }
    Not the prettiest looking thing but it does get the job done.
     

Share This Page