Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

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

i want to find how many ',' is present in a given string
any direct method and to seperate small strings from it
0
Kumudini
Asked:
Kumudini
  • 2
  • 2
  • 2
  • +3
1 Solution
 
desmondliuCommented:
#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
 
KumudiniAuthor Commented:
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
 
KumudiniAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
FrancozCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
fl0ydCommented:
#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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
FrancozCommented:
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
 
bcladdCommented:
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!

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now