Substring in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aramakri
    New Member
    • Dec 2007
    • 9

    Substring in C++

    I have "hello i am fine" as a single string.I want to store hello,i,am,fine in separate variables.How do i do that.Please help me
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by aramakri
    I have "hello i am fine" as a single string.I want to store hello,i,am,fine in separate variables.How do i do that.Please help me
    Try to use strstream in C++ stl for this.
    You can achieve what you want

    Thanks
    Raghuram

    Comment

    • aramakri
      New Member
      • Dec 2007
      • 9

      #3
      Any other way to do it Raghu instead of strstream stl

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by aramakri
        Any other way to do it Raghu instead of strstream stl
        then you have to go for C library the strtok function.
        I cant remember any other way in c++

        Thanks
        Raghuram

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by aramakri
          Any other way to do it Raghu instead of strstream stl
          You are supposed to use stringstream in C++ for this.

          You can't use the C strtok because a) it only works for C strings and b) prevents your code from being multithreaded.

          The entire C strings library is depreacated in C++. It is supported by C++ compilers only so ancient code can still compile but you are not to write new code using this obsolete library.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            If string s1="Hello I am fine" and string s2=(s1,0.4) string s2 should ="Hello"
            Code:
            cout<<s2<<endl;
            //should print "Hello"
            use
            Code:
            #include<cstring>

            Comment

            Working...