Finding the first non-white space character using sscanf (C++ equivalent)

D

Dilip

I have piece of code that breaks apart a string filled with spaces into
distinct strings each containing the apporpriate non-whitespace
characters using sscanf

char encodedsymbol[] = "IBM RRR XO";
char first[20], second[20], third[20];

sscanf(encodedsymbol, "%s%s%s", first, second, third);

Is there a C++ way to do this?

I had a little templated code that tokenizes a string based on a
delimiting character like this:

template<typename Str, typename Separator, typename Container>
void str_tokenize(const Str& str, const Separator delim, Container&
cont)
{
std::stringstream stream(str);
Container::value_type token;
while (!std::getline(stream, token, delim).fail())
cont.insert(cont.end(), token);
}

(where Container is almost always a vector<string> type)

I know I can modify this slightly to account for spaces... but I was
just wondering if there was a more straightforward C++ way to do it?
 
D

Dilip

I have piece of code that breaks apart a string filled with spaces into
distinct strings each containing the apporpriate non-whitespace
characters using sscanf

char encodedsymbol[] = "IBM RRR XO";
char first[20], second[20], third[20];

sscanf(encodedsymbol, "%s%s%s", first, second, third);

Is there a C++ way to do this?

Found it myself.

string ess(encodedsymbol);
istringstream iss(ess);
string first, second, third;

iss >> first >> second >> third;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top