How to split 2 varibles using delimiter.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 35
Reputation: RohitSahni is an unknown quantity at this point 
Solved Threads: 0
RohitSahni RohitSahni is offline Offline
Light Poster

How to split 2 varibles using delimiter.

 
0
  #1
Nov 8th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to split 2 varibles using delimiter.

 
0
  #2
Nov 8th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,679
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to split 2 varibles using delimiter.

 
0
  #3
Nov 8th, 2007
Or you can use stringstream and getline with ':' as the delimiter. That's generally easier to get right:
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. std::stringstream in ( "A:B:C" );
  8. std::string token;
  9.  
  10. while ( std::getline ( in, token, ':' ) )
  11. std::cout<<"Split token: "<< token <<'\n';
  12. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 35
Reputation: RohitSahni is an unknown quantity at this point 
Solved Threads: 0
RohitSahni RohitSahni is offline Offline
Light Poster

Re: How to split 2 varibles using delimiter.

 
0
  #4
Nov 12th, 2007
Can anyone plz provide me with the sample code. Above one is not working for me.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,679
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to split 2 varibles using delimiter.

 
0
  #5
Nov 12th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 35
Reputation: RohitSahni is an unknown quantity at this point 
Solved Threads: 0
RohitSahni RohitSahni is offline Offline
Light Poster

Re: How to split 2 varibles using delimiter.

 
0
  #6
Nov 13th, 2007
I was able to do it like this.

  1. #include<iostream.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. void splitString(char inputstr[100])
  6. {
  7. clrscr();
  8. // char inputstr[100],
  9. char str[10][20];
  10. // gets(inputstr);
  11. int i=0,j=0,k=0,l;
  12. while(inputstr[i]!='\0')
  13. {
  14. for(l=0;l<20;l++)
  15. {
  16. str[j][l]='\0';
  17. }
  18. str[j];
  19. while(inputstr[i]!=':')
  20. {
  21. str[j][k++]=inputstr[i++];
  22. if(inputstr[i]=='\0')
  23. {
  24. str[j][k+1]='\0';
  25. goto label;
  26. }
  27. }
  28. str[j][k+1]='\0';
  29. k=0;
  30. j++;
  31. i++;
  32. }
  33. label:
  34. for(i=0;i<=j;i++)
  35. {
  36. puts(str[i]);
  37. cout<<"\n";
  38. }
  39. getch();
  40. }
  41. void main()
  42. {
  43. char inputstr[100];
  44. gets(inputstr);
  45. splitString(inputstr);
  46. }


If u can provide me with wat u have done, that will be help ful.
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,861
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How to split 2 varibles using delimiter.

 
1
  #7
Nov 14th, 2007
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 void main() . 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 fgets() - 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.

regards Niek
Last edited by niek_e; Nov 14th, 2007 at 7:25 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 35
Reputation: RohitSahni is an unknown quantity at this point 
Solved Threads: 0
RohitSahni RohitSahni is offline Offline
Light Poster

Re: How to split 2 varibles using delimiter.

 
0
  #8
Nov 14th, 2007
Yup Thanks,
I already updated it. Actually that was just a rough code .
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to split 2 varibles using delimiter.

 
0
  #9
Nov 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: How to split 2 varibles using delimiter.

 
0
  #10
Nov 14th, 2007
Don't say int main( void ) either. The main() 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC