Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with parsing the string

Status
Not open for further replies.

TanyaB

Programmer
Aug 15, 2001
18
IL
Hi all,
please help me with this issue:
My program get Oracle connect string that looks like this:
username/password@dbinstance
How can I parse it to 3 strings( username,password,instance) ?
 
I would look into the function strstr, that should pull it out for you. For the search string use "//" and "@"

the return value is where the character string appears. From there it is just a bit of string manipulation with strncpy and a bit of pointer arithmetic.

for example: username would look like

char* password = strstr(yourString,"//");

char username[32];
strncpy(username,yourString,password-yourString-1);

Matt

 
how about..
char connection[100];
strcpy(connection, "username/password@dbinstance"); /* some sample string */
char *username, password, dbinstance;
username = &connection[0];
password = strchr(username, '/');
*password++ =0;
dbinstance= strchr(password, '@');
*dbinstance++ =0;
thats it :)
-hth,
shail
 
The strtok function is probably the best approach:

char *username;
char *password;
char *instance;
char yourString[500];

strcpy(yourString, passedConnectString);
username = strtok(yourString, "/@");
password = strtok(NULL, "/@");
instance = strtok(NULL, "/@");

There are a couple of caveats:

1. The contents are yourString are touched by strtok. It replaces the separators with a NULL.
2. username, password, and instance are merely pointing into the data space of yourString.
3. The code really should be checking for a return of NULL which means strtok couldn't find any more embedded separators.

 
Try this program it should handle what you want to do elegantly !

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define  _MAXSTRING_  50

typedef char String[_MAXSTRING_];

int Tokenise( char *buff, char *sep, char tokeniseword[][_MAXSTRING_] )
{
	int i = 0, l = 0, j = 0, k = 0;
	char bkp[_MAXSTRING_];

	while( buff[i] != '\0' )
	{
       for( k = 0; sep[k] != '\0'; k++ )
	   {
	      if( buff[i] == sep[k] )
		  {
              bkp[l] = 0;
		      if( strlen( bkp ) != 0)
			  {
			     strcpy( tokeniseword[j], bkp );
                 j++;
			     l = 0;
				 bkp[0] = 0;
			  }
		  }
          else
		  {
             bkp[l] = buff[i];
             l++;
		  }
		  i++;
	   }
	}

    bkp[l] = '\0';

	if(strlen( bkp ) != 0 ) 
	{
        strcpy( tokeniseword[j], bkp );
		j++;
	}

	return j;
}

void main()
{
	char *string = &quot;username/password@dbinstance&quot;;
	char *sep = &quot;/@&quot;;
	String *token;
	int i;
	int toknum;
	token  = ( String* )malloc( sizeof(String) * 3 );
	toknum = Tokenise( string, sep, token );
	for( i = 0; i < toknum; i++ )
	{
		printf(&quot;%s&quot;,token[i]);
		if( i != toknum - 1 )
		{
			printf(&quot;,&quot;);
		}
	}
	printf(&quot;\n&quot;);
    free(token);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top