Tuesday, July 14, 2009

C++ string functions help?

I need to write a library similar to the %26lt;string.h%26gt; header file. I need help writing the following functions. They are included in my own class called "string".





void resize(unsigned int, char);


//resizes character array "Buffer" to number specified by


//first parameter


//fills extra space by the second parameter character





void insert(unsigned int, string %26amp;);


//Inserts a string into "Buffer" a character array.


//First parameter: Place to insert


//Second parameter: string to insert.








void remove(unsigned int, unsigned int);


//removes characters from "Buffer", a character array.


//First parameter : Starting position,


//Second parameter : number of characters to remove.

C++ string functions help?
Okay, this is pretty obviously homework. I won't show any code here, but I'll give you an idea how to implement the functions listed.





First, void resize(unsigned int, char); It has to do these things in approximately this order:


Validate int as greater than zero, or the operation is over before it starts. Next, it needs to allocate memory for a new Buffer with the extra space. Oh-oh. How do you find the buffer if it doesn't return a pointer or a reference to it? Better change the return type to char%26amp; or char *. Okay, now the new characters are written into the new buffer and then the old characters ... Oh-oh, we need to know where that things at too. Better include a reference or pointer to the old buffer as a parameter. So we take the old buffer and move the old characters over into the new buffer behind(?!) the new characters. Rats! Do we need to consider that the new characters should always be prepended or appended or does it change?





void insert(unsigned int, string %26amp; old_string, string %26amp; new_string); maybe?





void remove(unsigned int, unsigned int, string %26amp; buffer);





Unless you MUST follow these exact prototypes for your assignment, you may want to consider these modifications.





I think you can work these last two from the first walk through I gave you. They aren't really any different as to the mechanics of the problem, just what information is needed to operate from. Good luck!
Reply:I can understand that. Next time post partial code with specific questions and I (we?) can take the sting out of those sticky points. Sorry to be a drag. Cheers! Report It

Reply:Sounds like homework to me...


And if you can't do these simple things, you are in trouble!


Do it as a for(i=..;i%26lt;..;i++) loop, and use Buffer[i].


(or review the use of pointers: that's the beauty of C)


No comments:

Post a Comment