471,092 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

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.


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.

Jul 22 '10 #1
4 1540
207 100+
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.
Jul 22 '10 #2
542 512MB
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]
Jul 23 '10 #3
2,425 Expert 2GB
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").
Jul 23 '10 #4
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(switchport, "%20[^/] /%s", vSwitch, portgroup) );
printf("vSwitch = \"%s\"\n", vSwitch);
printf("portgroup = \"%s\"\n", portgroup);
return 0;
Jul 23 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Adrian | last post: by
9 posts views Thread by Divick | last post: by
5 posts views Thread by Allerdyce.John | last post: by
8 posts views Thread by Paul Craig | last post: by
2 posts views Thread by Alan Bak | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.