943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7637
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2007
0

How to split 2 varibles using delimiter.

Expand Post »
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.
Similar Threads
Reputation Points: 25
Solved Threads: 0
Light Poster
RohitSahni is offline Offline
35 posts
since Jul 2007
Nov 8th, 2007
0

Re: How to split 2 varibles using delimiter.

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 8th, 2007
0

Re: How to split 2 varibles using delimiter.

Or you can use stringstream and getline with ':' as the delimiter. That's generally easier to get right:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2007
0

Re: How to split 2 varibles using delimiter.

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

Thanks in advance
Reputation Points: 25
Solved Threads: 0
Light Poster
RohitSahni is offline Offline
35 posts
since Jul 2007
Nov 12th, 2007
0

Re: How to split 2 varibles using delimiter.

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 13th, 2007
0

Re: How to split 2 varibles using delimiter.

I was able to do it like this.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 25
Solved Threads: 0
Light Poster
RohitSahni is offline Offline
35 posts
since Jul 2007
Nov 14th, 2007
1

Re: How to split 2 varibles using delimiter.

I strongly suggest that you try Narue's option. What didn't work with that code?
Quote ...
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.
Last edited by Nick Evan; Mar 26th, 2010 at 10:39 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 14th, 2007
0

Re: How to split 2 varibles using delimiter.

Yup Thanks,
I already updated it. Actually that was just a rough code .
Reputation Points: 25
Solved Threads: 0
Light Poster
RohitSahni is offline Offline
35 posts
since Jul 2007
Nov 14th, 2007
0

Re: How to split 2 varibles using delimiter.

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 14th, 2007
0

Re: How to split 2 varibles using delimiter.

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Calculating Time in sec
Next Thread in C++ Forum Timeline: I'm stumped, please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC