473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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.

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.
Jul 22 '10 #1
4 1807
hype261
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
whodgson
542 512MB
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]
Jul 23 '10 #3
donbock
2,426 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

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

Similar topics

2
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
2
by: Max Adams | last post by:
Question about using delegates with a string paramater to return a ListViewItem object... All, I have a thread and I want this thread to post messages to the main GUI thread using a delegate....
3
by: Adrian | last post by:
Hi is it possible (if so how) to do a redirect / url forward with query string i.e. www.test1.com?pram1=a?pram2=b is forwarded to www.test2.com?pram1=a?pram2=b
9
by: Divick | last post by:
Hi all, I have a problem related to std::string class. Is it ok to assign a global string variable to a local string object as shown below? I am trying to print the address of local string...
5
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError:...
8
by: Paul Craig | last post by:
Hi, I am currently able to open forms given a string variable using the following code: Dim strForm As String = "Form1" Dim theForm As Form theForm =...
5
by: niceliang84 | last post by:
Hi, i am currently doing a c++ program using Borland C++. now i am facing a problem where i want to compare the keyword input from the user with the string that read from file. this is a part of...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
2
by: Alan Bak | last post by:
I have a text file that I am reading in with perl line by line and breaking into variables for processing. The lines are being broken on white spaces using split. Here are a couple of lines from...
10
bajajv
by: bajajv | last post by:
Hi, I was trying to implement CString with std::string and std::wstring. class CString { public: std::string str; CString() {str = "ABCD";} //this works }: class CString {
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.