need help with splitting a string into two and assign them to two variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Grer
    New Member
    • Jul 2010
    • 2

    need help with splitting a string into two and assign them to two variables

    I need to write a short program in C to split a string value into two and assign them to two different variables.

    Example:

    Original string is "Switch1/Port1"

    split the string at the "/" and assign them to:

    first variable = Switch1
    second variable = Port1

    I have read about find_last_of and find_first_of functions but not quiet understand how they work.

    Any help would greatly appriciated.

    Thanks,
    Bob.
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    String split

    Basically you are going to use two string functions to split the string. First is find_first_of as you mentioned and the second is substr.

    find_first_of will return the position in the string where it detected the char. The method find_first_of is overloaded, but the method you are probably going to use is...

    size_t find_first_of ( const char* s, size_t pos = 0 ) const;

    The first parameter is the char you are looking for and the second parameter is the position to start looking after.

    You will need to check that the return value of the find_first_of method is not npos which tells you the char you are looking for isn't in the string.

    Once you have the position of the char you are looking for you will need to use substr method twice to split the string.

    string substr ( size_t pos = 0, size_t n = npos ) const;

    The first parameter is the index to start the split at and the second parameter is the number of characters you want.

    Once you code the solution and if you are still having problems post it here and somebody will help you out.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Yes...
      In C a string is an array in which the last character is \0 or NULL.
      One way to handle this is to put your string into an array.
      Copy the characters in this array (up to '/')into a second array. Now find '\' in the original array and replace it with " ".
      If you need to you can move all the characters down so that the first char is at array[0]

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        If your starting string "Switch1/Port1" is in writeable memory (that is, it is not a string literal) and if you don't mind corrupting that string then you have a tricky option.

        You start with a char pointer that points to the beginning of the starting string. Find the "/" character and change it into a null ("\n"). Then set a second char pointer to the next slot in the starting string (that is, the "P").

        Comment

        • Bob Grer
          New Member
          • Jul 2010
          • 2

          #5
          Thanks for the responds. They are very useful ideas.

          I found a function "sscanf" that is very simple to do exactly what I was looking for. Here is a sample,

          include <stdio.h>
          int main()
          {
          char switchport[] = "vswitch1/12";
          char vSwitch[20], portgroup[20];

          if ( sscanf(switchpo rt, "%20[^/] /%s", vSwitch, portgroup) );
          {
          printf("vSwitch = \"%s\"\n", vSwitch);
          printf("portgro up = \"%s\"\n", portgroup);
          }
          return 0;
          }

          Comment

          Working...