|
From: Abhijit S. <mu...@gm...> - 2002-12-28 20:14:14
|
Greets, Danny.
----- Original Message -----
From: Danny Roelofs
To: dev...@li...
Sent: Sunday, December 29, 2002 12:26 AM
Subject: [Dev-C++] trim / sub string
Hi,
Another problem to solve.. i wish to make a routine which gives me a
substring or trimmed down on the following
conditions that i can start from anypoint in the string untill the offset is
reached. so for example
mytext = "this is a test";
result = substring(mytext,6,2);
result would now have to contain the 2nd word "is"...
--------------------------------
You can use pointer arithmetic to reach characters. For example,
mytext + 5
will access the string from `is' onwards (uptil the NULL character).
To copy it into another buffer, you need to copy from the above location
until the number of characters specified have been exhausted. For example,
char *substring(const char *orig, char *buf, int offset, int len)
{
for(unsigned i = 0; i < len; ++i)
buf[i] = orig[i + offset];
buf[i] = 0; // NULL terminator
return buf; // For convenience
}
This is a zero-based function, so
mytext = "this is a test";
substring(mytext, result, 5, 2);
Would give a resultant buffer containing `is'.
HTH.
___________________________________________________________
Abhijit Shylanath
E-mail: mu...@gm... || ibr...@bi...
Web-site: http://mudeth.tripod.com/
|