By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,574 Members | 2,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,574 IT Pros & Developers. It's quick & easy.

How to find whether there are any consecutive commas in a string?

P: n/a
I need a simple method to find whether there are any instances of consecutive
commas (more than 1) in a given string without parsing each character of the
string. I tried with strtok() with comma as separator but it considers all
consecutive commas as a single separator and gives the next token.
Is there any simple method to do the same?
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Vishal wrote:
I need a simple method to find whether there are any instances of
consecutive commas (more than 1) in a given string without parsing
each character of the string. I tried with strtok() with comma as
separator but it considers all consecutive commas as a single
separator and gives the next token.
Is there any simple method to do the same?


Just do it. Whatever method you use will have to "parse" each character of
the string - it's simply unavoidable.

bool two_commas(const char* psz)
{
char prev = 0;
while (*psz)
{
if (*psz==prev && prev==',')
return true;
prev = %psz++;
}
return false;
}

You could also use strstr:

bool two_commas(const char* psz)
{
return strstr(psz,",,") != 0;
}

Analogous constructs could be used with CString or std::string. It's likely
that the simple lopp through the characters will be the fastest of these.

-cd
Nov 17 '05 #2

P: n/a
Carl Daniel [VC++ MVP] wrote:
prev = %psz++;


should be prev = *psz++;

-cd

Nov 17 '05 #3

P: n/a
Hey Carl

Must be all those tracking refs you've been playing with of late ;-)

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
http://blog.voidnish.com /* My blog on C++/CLI, MFC, Whidbey, CLR... */
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
prev = %psz++;


should be prev = *psz++;

-cd

Nov 17 '05 #4

P: n/a
Nishant S wrote:
Hey Carl

Must be all those tracking refs you've been playing with of late ;-)


If only I could use such an excuse! So far, nearly all of my playing with
tracking refs has been in my imagination.

-cd
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.