Thursday, July 9, 2009

How do you go about solving this basic C++ strings problem...?

Say I have the string





NewYork;NewJersey





I want to put in a string everything up to the point that it reaches a ;





So in this case I would like a string to have NewYork





Can anyone write a small program that will do this?





Thanks in advance!

How do you go about solving this basic C++ strings problem...?
//--------------------------------------...


// StrT: Type of string to be constructed


// Must have char* ctor.


// str: String to be parsed.


// delim: Pointer to delimiter.


// results: Vector of StrT for strings between delimiter.


// empties: Include empty strings in the results.


//------------------------------------...


template%26lt; typename StrT %26gt;


int split(const char* str, const char* delim,


vector%26lt;StrT%26gt;%26amp; results, bool empties = true)


{


char* pstr = const_cast%26lt;char*%26gt;(str);


char* r = NULL;


r = strstr(pstr, delim);


int dlen = strlen(delim);


while( r != NULL )


{


char* cp = new char[(r-pstr)+1];


memcpy(cp, pstr, (r-pstr));


cp[(r-pstr)] = '\0';


if( strlen(cp) %26gt; 0 || empties )


{


StrT s(cp);


results.push_back(s);


}


delete[] cp;


pstr = r + dlen;


r = strstr(pstr, delim);


}


if( strlen(pstr) %26gt; 0 || empties )


{


results.push_back(StrT(pstr));


}


return results.size();


}








Examples:





// using CString


//------------------------------------...


int i = 0;


vector%26lt;CString%26gt; results;


split("a-b-c--d-e-", "-", results);


for( i=0; i %26lt; results.size(); ++i )


{


cout %26lt;%26lt; results[i].GetBuffer(0) %26lt;%26lt; endl;


results[i].ReleaseBuffer();


}





// using std::string


//------------------------------------...


vector%26lt;string%26gt; stdResults;


split("a-b-c--d-e-", "-", stdResults);


for( i=0; i %26lt; stdResults.size(); ++i )


{


cout %26lt;%26lt; stdResults[i].c_str() %26lt;%26lt; endl;


}





// using std::string without empties


//------------------------------------...


stdResults.clear();


split("a-b-c--d-e-", "-", stdResults, false);


for( i=0; i %26lt; stdResults.size(); ++i )


{


cout %26lt;%26lt; stdResults[i].c_str() %26lt;%26lt; endl;


}
Reply:I want to put in a string everything up to the point that it reaches a ;





So in this case I would like a string to have NewYork


????





Your question does not make any sense
Reply:Use the strtok function to tokenize the string around the ;





# include %26lt;iostream%26gt;


# include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main (void)


{


char str[ ] = "NewYork;NewJersey;


char *pch;





pch = strtok(str, ";"); //Takes the string str and tokenizes it around the delimiter that was specified.





printf("%s\n", pch);





return 0;


}
Reply:i am writing here very simple code segment of basic level that would not take in to consideration complex phenominon involved like memory allocation etc. just for the sake of simplicity


#include %26lt;conio.h%26gt;


#include %26lt;stdio.h%26gt;





int main()


{


char YourString[]= "NewYark;NewJersey";


char OutString[100]; //we asume the string is less than 100 characters





int i=0;





while(true) //infitie loop is controlled by a break statement in block


{


if(YourString[i]==0 || YourString[i]==';') //end of string of or a semicolon?


{


OutString[i]=0; //place a string terminator null to out string


break; // and break the loop


}


else


{


OutString[i]=YourString[i];// this character is to be included in outpu string


i++; incres index


}


printf("\nInput String:%s, Output string=%s",YourString,OutString);


getch()


return 0;


}





this will serve your purpose elegantly. But for string, there are many memory allocation problems are involved which I just overlooked for the sake of simplicity and focused on main problem. Hope that answers the questions. If you still have any query, feel free to write at khan10200@yahoo.com





Thanks


No comments:

Post a Comment