Link to home
Start Free TrialLog in
Avatar of Kumudini
Kumudini

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of desmondliu
desmondliu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kumudini
Kumudini

ASKER

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
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++;
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;
}    
Avatar of Mayank S
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.
#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
>> 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.
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...:)
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 (https://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer