🔒 Locked
split a string with string.h
Advertisement
Original Post
is.getline(s1,100,'' '');
temps1 = (char*)memchr(s1,'','',sizeof(s1));
if ( temps1 != NULL )
count << temps1-s1+1
I got that code which gives me the end location
s1 is the string ( char *)
temps1 is the ptr to the comma
so I want the first 3 chars like say 31 to 34 in memory, is there a function that will create a char* from those locations?
temps1 = (char*)memchr(s1,'','',sizeof(s1));
if ( temps1 != NULL )
count << temps1-s1+1
I got that code which gives me the end location
s1 is the string ( char *)
temps1 is the ptr to the comma
so I want the first 3 chars like say 31 to 34 in memory, is there a function that will create a char* from those locations?
quote:
Original post by nick5454
is.getline(s1,100,' ');
temps1 = (char*)memchr(s1,',',sizeof(s1));
if ( temps1 != NULL )
count << temps1-s1+1
I got that code which gives me the end location
s1 is the string ( char *)
temps1 is the ptr to the comma
so I want the first 3 chars like say 31 to 34 in memory, is there a function that will create a char* from those locations?
#include <vector>
using std::vector;
vector<string> strchop(string s,char delim = ','){
unsigned int i = 0;
vector<string> ret;
string tmp;
while(i < s.size()){
if(s[i] == delim){
ret.push_back(tmp);
tmp = "";
} else tmp += s[i];
i++;
}
if(tmp != "") ret.push_back(tmp);
return ret;
}
This seems to be the most elegant solution that comes to mind. Just run strchop("i,like,cookies!!") and you'll get the appropriate vector.
[edited by - Ishan on May 9, 2003 2:42:19 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement