How a c string can be splitted by token '\' using strtok?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    How a c string can be splitted by token '\' using strtok?

    How a c string can be splitted by token '\' using strtok?
    Example:
    Hello\world is one c-string. I need to split it into Hello and world. How this can be implemented? Thanks in advance.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Probably this link may help you.....

    STRTOK Link

    Regards
    Dheeraj Joshi

    Comment

    • Man4ish
      New Member
      • Mar 2008
      • 151

      #3
      I know abt this but it does not split it '\' strtok. Even i cannot compile it.

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        The link was given for your reference not to copy the code from the that page.

        Regards
        Dheeraj Joshi

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          strtok(s1,s2) returns the next token in s1 using the delimiters specified in s2.
          The above link explains this in more detail.
          Presumably in your example the' \' is the delimiter.

          Comment

          • Man4ish
            New Member
            • Mar 2008
            • 151

            #6
            Following is the error when "\" is used as delemiter for the example given by dhiraj.
            check.cpp:8:21: warning: missing terminating " character
            check.cpp:8: error: missing terminating " character
            check.cpp: In function ‘int main()’:
            check.cpp:9: error: expected primary-expression before ‘char’
            check.cpp:9: error: expected ‘,’ or ‘;’ before ‘char’
            check.cpp:10: error: ‘result’ was not declared in this scope


            Code:
            #include <iostream>
            #include <string.h>
            using namespace std;
            
            int main()
            {
                char str[] = "now # is the time for all # good men to come to the # aid of their country";
                char delims[] = "\";             //// error because of this
                char *result = NULL;
                result = strtok( str, delims );
                while( result != NULL ) {
                printf( "result is \"%s\"\n", result );
                result = strtok( NULL, delims );
                }
                return 0;
            }
            ~
            Last edited by Banfa; Nov 5 '09, 09:22 AM. Reason: Added [Code] ... [/code] tags

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              You need to escape backslash in character string with backslash like you do it with quote five lines below.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                newb16 is, I think, pointing out that there is a clash between the delimiters in the string ("#") and the 'char delims []' declaration ("/")

                Code:
                char *p=strtok(str," / "); /*This call is required to 
                identify the string you want to analyze as the s1 argument.                                                                                                                          
                Now loop to analyze the string and print out the tokens
                 (words)*/
                while (p){
                      cout<<p<<'\n'; 
                     p= strtok(NULL,"/ ");
                }
                I haven't compiled or debugged this but this is my understanding of how the function works.

                Comment

                • Tassos Souris
                  New Member
                  • Aug 2008
                  • 152

                  #9
                  going back to the theory every one using C should know, the '\' character is used to excape other characters giving them special meaning.
                  for example:
                  '\n' means the new line character
                  '\t' the tab character
                  and so on....

                  since the '\' has a special meaning for C you must escape it to have the character '\'.
                  So if you need:
                  Code:
                  char delims[] = "\\";

                  Comment

                  • whodgson
                    Contributor
                    • Jan 2007
                    • 542

                    #10
                    @Tassos Souris
                    What you say is generally true but not so in this case as s2 is any delimiter's place holder as part of the strtok() function

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by whodgson
                      @Tassos Souris
                      What you say is generally true but not so in this case as s2 is any delimiter's place holder as part of the strtok() function
                      You have misread the OP's question: he wants to use the backslash (\) as a delimiter. However, in C, the backslash is an escape character. Ergo, the line char delims[] = "\"; causes an error because the backslash is escaping the closing quote. Tassos' solution would work.

                      Comment

                      Working...