473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
4 1519
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
Carl Daniel [VC++ MVP] wrote:
prev = %psz++;


should be prev = *psz++;

-cd

Nov 17 '05 #3
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
1
by: jcabc | last post by:
I have a text string which has multiple values seperated by commas. My goal is to find the postion of the 4th comma and then read the text after that comma until the next comma using VBA. Ex. ...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
24
by: shafique | last post by:
Hello, Can anybody have idea as how to find it there exist consective one's in a word/dword using bitwise operatiors in C language. Obviously this can be done by shifting and looping one by...
0
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). ...
6
by: rcamarda | last post by:
Hello, I need to find students that have 4 consecutive absences. When a student is absent 4 times in a row, they can be dropped from the class. My class attendance file contains each attendance...
10
by: QQ | last post by:
for instance, I read a char from the input and I need to decide whether it is a letter or a number What I am doing is char a; ...... // read a int true = false; if(( (a >='0') && (a <='9')) | |...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
3
by: Nelson | last post by:
Hi All, I want to look at what happens to stock prices after a certain number of consecutive up or down days. Let's say, for instance, I'd like to see where a stock's price is 5 days after a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.