Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
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.
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
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?
 

ChrisA

macrumors G5
Jan 5, 2006
12,905
2,150
Redondo Beach, California
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';
}
 

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.