PDA

Click to See Complete Forum and Search --> : Manipulating char variables


Master
Jun 23rd, 2000, 07:09 AM
What are some of the functions (with syntax) for manipulating char variables? Like in VB there's trim() and mid() and left() and all those great functions... what would i do in c++ to accomplish these types of functions?

parksie
Jun 23rd, 2000, 05:43 PM
don't you mean char*?
at the moment, for pure operations on char arrays, then string.h has some useful things, all beginning with str.
If you want ease of use, try the STL string class:


#include <iostream>
#include <string>

using namespace std;

void main() {
string sMyStr = "A string";
cout << sMyStr << endl;
}


there's lots of member functions to do what you need - the documentation's pretty good.

Dillinger4
Jun 25th, 2000, 07:41 AM
#include <string.h>

// example of using some string functions

class Cmessage {
char* pmessage //pointer to object text string

public:
// function to display message

void showit();
{
cout << endl << pmessage;
}
// constructor defination

Cmessage(const char* text = "default message")
{
pmessage = new char[strlen(text) + 1]; // allocate space
// for text
strcpy(pmessage,text); // copy text to new memory

~Cmessage(); //destructor protype
};






// deconstructor to free up memory created by new
Cmessage::~Cmessage()
{
cout << "destructor called"; // just to track what happens
<< endl;
delete[] pmessage; // free memory assigned to pointer
}


"A man ought to read as inclination leads him for what he reads as a task will do him little good"