?
Solved

i want to find how many charcters of some x type is present in a CString and seperate string left  to that x char

Posted on 2003-03-31
10
Medium Priority
?
172 Views
Last Modified: 2010-04-01
i want to find how many ',' is present in a given string
any direct method and to seperate small strings from it
0
Comment
Question by:Kumudini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 

Accepted Solution

by:
desmondliu earned 80 total points
ID: 8243629
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="sample string,just testing,thanks";
  char * pch;
  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str,",");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, ",");
  }
  return 0;
}
0
 

Author Comment

by:Kumudini
ID: 8244072
There is no direct solution/function to take care of it.
I have also done the same way using Find function of C++ CString class,Thank You
0
 

Author Comment

by:Kumudini
ID: 8244091
CString str,str1,strFileNames;
CStringArray strFileName;
str1 = strFileNames;
int nT = 0;
int n = str1.Find(",",0);
while(n != -1)
{
  n = str1.Find(",",0);
  if(n > 0)
 {
  str = str1.Left(n);
  strFileName.Add(str);
  nT++;
  str1.Delete(0,n+1);
 }
}
strFileName.Add(str1);
nT++;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:Francoz
ID: 8244609
void main()
{

int count = 0;
char *str = new char[10];
char* del = str; //For deletion purpose

cin >> str;

while(*str)
{
if(*str == ';')
count++;
str++;
}

cout <<count;
delete []del;
}    
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8245224
desmondliu,

Why do you need to use strtok () and modify the original string?? Its better to simply keep a counter and scan the string from left to right, like Francoz has done.

Mayank.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8247289
#include <string>
#include <list>

int main( int argc, char* argv[] ) {

    std::string = "This,is,my,string";

    std::string::size_type last = 0;
    std::string::size_type current = 0;

    std::list<std::string> str_list;

    do {
        current = str.find( ',', last );
        str_list.push_back( str.substr( last, current - last ) );
        last = current + 1;
    while( std::string::npos != current );

    // str_list contains tokens
    std::list::size_type n = str_list.size();
    // number of ',' == number of tokens - 1
    std::list::size_type n_character = n - 1;

    return 0;
}

As a side note: void main() isn't valid c++.

.f
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8251012
>> void main() isn't valid c++.

Ya, but some non-standard C++ compilers often compile it fine.... but anyways, int main () is the standard and is correct.

Mayank.
0
 
LVL 2

Expert Comment

by:Francoz
ID: 8259936
Yeah according to standards void main() is not a valid func header in C++. But the specifications of microsoft on OS design says even an app doesnt return a code to the OS at the exit point OS will take the code as zero for successful execution. This violates in case of not successful termination of the program only.

But I agree with floyd, It is required...:)
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9979152
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answereed: Points to desomondliu: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question