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.

Recommended Answers

All 11 Replies

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.

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';
}

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

Thanks in advance

>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 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.

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.
commented: Well said. +13

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

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.

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.

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. :)

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

Thanks .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.