954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to split 2 varibles using delimiter.

Hi all,

Suppose i have one string as "A:B:C", where ":" is the delimiter . now i want to seperate this A, B, and C and save them in 3 different variables.

How can i implement this thru C++ code.

Plz let me know.

Thanks in advance.

RohitSahni
Light Poster
35 posts since Jul 2007
Reputation Points: 25
Solved Threads: 0
 

using c++ std::string object use the find method to locate the ':' and then the substr method to extract all the characters from position 0 up to but not including the colon.

I'm not going to write it for you, so post the code you have done.

Ancient Dragon
Retired & Loving It
Team Colleague
30,041 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

Or you can use stringstream and getline with ':' as the delimiter. That's generally easier to get right:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  std::stringstream in ( "A:B:C" );
  std::string token;

  while ( std::getline ( in, token, ':' ) )
    std::cout<<"Split token: "<< token <<'\n';
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Can anyone plz provide me with the sample code. Above one is not working for me.

Thanks in advance

RohitSahni
Light Poster
35 posts since Jul 2007
Reputation Points: 25
Solved Threads: 0
 

>Can anyone plz provide me with the sample code.
I've done that. Now you provide us with proof that you've attempted to solve the problem.

>Above one is not working for me.
Then you're using it wrong. Post your attempt.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I was able to do it like this.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void splitString(char inputstr[100])
{
	clrscr();
  //	char inputstr[100],
	char str[10][20];
  //	gets(inputstr);
	int i=0,j=0,k=0,l;
	while(inputstr[i]!='\0')
	{
		for(l=0;l<20;l++)
	{
		str[j][l]='\0';
	}
		str[j];
		while(inputstr[i]!=':')
		{
			str[j][k++]=inputstr[i++];
			if(inputstr[i]=='\0')
			{
				str[j][k+1]='\0';
				goto label;
			}
		}
		str[j][k+1]='\0';
		k=0;
		j++;
		i++;
	}
	label:
	for(i=0;i<=j;i++)
	{
		puts(str[i]);
		cout<<"\n";
	}
	getch();
}
void main()
{
char inputstr[100];
gets(inputstr);
splitString(inputstr);
}

If u can provide me with wat u have done, that will be help ful.
Thanks.

RohitSahni
Light Poster
35 posts since Jul 2007
Reputation Points: 25
Solved Threads: 0
 

I strongly suggest that you try Narue's option. What didn't work with that code?
If u can provide me with wat u have done, that will be help ful.
Where to start..

- don't use <a href="http://www.research.att.com/~bs/bs_faq2.html#void-main">void main</a> () . Use int main(void) , that's how it's defined. (or int main(int argc, char* argv[])
- don't use conio.h . It's outdated
- don't use clrscr() . Not portable
- don't use goto. A loop can always do what goto does.
- don't use getch() use getchar() instead.
- instead of gets() use <a href="http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html">fgets</a> ()
- use logical var-names. If you're writing a big program you don't want vars like i,j,k,l,m,m1,m2 etc...
- if you use cout , put std:: infront of it. Or using namespace std ; above main.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Yup Thanks,
I already updated it. Actually that was just a rough code .

RohitSahni
Light Poster
35 posts since Jul 2007
Reputation Points: 25
Solved Threads: 0
 

You'd be amazed how much "rough code" makes it through to production.

Get it right first time, then you won't have to say "I'll fix it later, promise", then fail miserably to back up your statement.

The same goes for indenting, commenting, structuring etc. It takes less time to do it properly first time around than it would be to continually struggle with a massive blob of poorly indented and uncommented code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Don't say int main( void ) either. Themain() function actually does take arguments... but if you don't want to deal with them use C++, not some bad C abomination: int main()

When in Rome, do as the romans. When using C++, use C++ I/O.

Don't use getchar() (or any of the non-standard variations). Use std::cin.get() instead.
Don't use fgets() (or, *gasp*, gets()). Use std::getline() or std::cin.get() instead.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Guys i have made the neccasary changes, actually the code which i pasted earlier was the code written on very old version of C++(Borland). thats y those things were there but now the code is standardised so not a prob now.

Anyways Thank you all for showing so much enthu for this post.

Thanks once again. :)

RohitSahni
Light Poster
35 posts since Jul 2007
Reputation Points: 25
Solved Threads: 0
 

If anyone needs it let me know i will paste it.

Thanks .

RohitSahni
Light Poster
35 posts since Jul 2007
Reputation Points: 25
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You