7 Replies - 693 Views - Last Post: 25 March 2016 - 06:09 PM Rate Topic: -----

#1 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

C string handle to C++ string handle

Posted 25 March 2016 - 10:39 AM

C string handle

    const char delimiter[] = ",\t\n";
    char* token;
    
    //Get the first token
    token = strtok(str, delimiter); 

    while(token != NULL){
  
       printf("%s",token);
       token = strtok(NULL, delimiter);
    
    }




How can I do the same thing in C++?
I know it can do the same with C layout, but I want to learn something more like C++ :beta1:

And how can I read a string char by char?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: C string handle to C++ string handle

#2 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: C string handle to C++ string handle

Posted 25 March 2016 - 11:04 AM

View Postbrotherofninth, on 25 March 2016 - 10:39 AM, said:

C string handle

    const char delimiter[] = ",\t\n";
    char* token;
    
    //Get the first token
    token = strtok(str, delimiter); 

    while(token != NULL){
  
       printf("%s",token);
       token = strtok(NULL, delimiter);
    
    }




How can I do the same thing in C++?
I know it can do the same with C layout, but I want to learn something more like C++ :beta1:/>

And how can I read a string char by char?

Thanks in advance



Also, after reading a string char by char, how can i form them back to string?
Was This Post Helpful? 0
  • +
  • -

#3 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: C string handle to C++ string handle

Posted 25 March 2016 - 01:32 PM

You're driving the admins crazy. Before you click to reply, look about 4mm down and slightly to the left for the button that looks like this:
Attached Image
Use it.

Now to your question. Look through the documentation for the C++ string class. In particular, I think you'll like the functions find_first_of, find_last_of, find_first_not_of, and find_last_not_of.
Was This Post Helpful? 0
  • +
  • -

#4 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: C string handle to C++ string handle

Posted 25 March 2016 - 01:50 PM

Sorry for the messy reply and Thanks for all people who were helping me :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#5 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: C string handle to C++ string handle

Posted 25 March 2016 - 03:11 PM

I finally design a function, but it is not allow to get the final block of data

    string dd = "a, b, c, d, e";
    size_t pos = dd.find(',');
    string s;

    while(pos< dd.length()){
    
        s = dd.substr(0,pos);
        dd = dd.substr(pos+1);
        pos = dd.find(',');
        
        cout<<pos<<endl;
        cout<<dd.length()<<endl;
        cout<<s<<endl;
        
    }




I know that if the find(',') cannot find any ',', will return a huge number


Is that mean I can use an if statement to handle this?

This post has been edited by brotherofninth: 25 March 2016 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1860
  • View blog
  • Posts: 6,698
  • Joined: 19-February 09

Re: C string handle to C++ string handle

Posted 25 March 2016 - 03:21 PM

The last block does npt end in a comma I take it. So the last block remains in the string dd.
Was This Post Helpful? 0
  • +
  • -

#7 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: C string handle to C++ string handle

Posted 25 March 2016 - 03:29 PM

View Post#define, on 25 March 2016 - 03:21 PM, said:

The last block does npt end in a comma I take it. So the last block remains in the string dd.

Ah, Thanks mate :bananaman:
Was This Post Helpful? 0
  • +
  • -

#8 CordovaCode   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 125
  • Joined: 14-March 16

Re: C string handle to C++ string handle

Posted 25 March 2016 - 06:09 PM

I stripped my comments out of this, read through it to see if you can tell what is happening. I think you might find something that will help.

#include <string>
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
 
    std::vector<std::string> words;
    std::ifstream in("sample.txt");
    std::string word;
    while(in >> word)
        words.push_back(word);

    for(int i = 0; i < words.size(); i++)
    {
        std::string temp_string(words[i]);
        for (int j = 0, len = temp_string.size(); j < len; j++)
        {
            if (ispunct(temp_string[j]))
            {
                temp_string.erase(j--, 1);
                len = temp_string.size();
            }
        }
        words[i] = temp_string;
    }

    for(int i = 0; i < words.size(); i++)
    {
        std::cout << words[i] << " : " << words[i].length() << std::endl;
    }

}

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1