superleo
asked on
Splite string into varaibles.
Hello all,
How can I splite a string into several variables, for example.
splite "4+7" into 2 integer and 1 string (4, 7 and "+".
Thank you.
How can I splite a string into several variables, for example.
splite "4+7" into 2 integer and 1 string (4, 7 and "+".
Thank you.
ASKER
Thank you, in fact my object to convert a string (maths formula) into 3 variables. For example, 32+6 into 32, 6, and "+". How can I save them into 2 integer and 1 string by using "type"??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh, and one more line at the end of the while:
set lasttype to type of "current" character
set lasttype to type of "current" character
2 superleo
int atoi( const char *string ); ??
>...by using "type"??
what do you mean?
int atoi( const char *string ); ??
>...by using "type"??
what do you mean?
say you want to convert the string "32+6" (assuming no spaces before and after the plus sign). Suppose this string is stored in a variable strFormula
char oper[2];
int v1,v2;
char *strFormula="32+6";
sscanf(strFormula,"%d%c%d" ,&v1,&oper [0],&v2);
oper[1]=0;
printf("%d%s%d\n",v1,oper, v2);
char oper[2];
int v1,v2;
char *strFormula="32+6";
sscanf(strFormula,"%d%c%d"
oper[1]=0;
printf("%d%s%d\n",v1,oper,
assuming that your string has a lot of variations, like
32+2
32+ 2
32 +2
32 + 2
32 +(2)
ets.
I suggest to use either regexec() and regcomp(), or to implement the parser with (f)lex.
32+2
32+ 2
32 +2
32 + 2
32 +(2)
ets.
I suggest to use either regexec() and regcomp(), or to implement the parser with (f)lex.
Use strtok()
Here is a function that will split a string into separate variables (char **) and return the number of entries.
#include <stdio.h>
#include <string.h>
#include <split.h>
int split(char * pstrSource, char ** pstrDest, char * pstrSeps)
{
auto int intNumTokens=0;
auto char * pstrToken=NULL;
static char * pstrDefaultSeps="\t,; ";
static char * pstrSepsToUse=NULL;
if (!pstrSource || !pstrDest)
{
return 0;
}
pstrSepsToUse=pstrSeps;
if (!pstrSepsToUse)
{
pstrSepsToUse=pstrDefaultS eps;
}
pstrToken = strtok( pstrSource, pstrSepsToUse );
while( pstrToken != NULL )
{
pstrDest[intNumTokens]=pst rToken;
intNumTokens++;
pstrToken = strtok( NULL, pstrSepsToUse );
}
return intNumTokens;
}
#include <stdio.h>
#include <string.h>
#include <split.h>
int split(char * pstrSource, char ** pstrDest, char * pstrSeps)
{
auto int intNumTokens=0;
auto char * pstrToken=NULL;
static char * pstrDefaultSeps="\t,; ";
static char * pstrSepsToUse=NULL;
if (!pstrSource || !pstrDest)
{
return 0;
}
pstrSepsToUse=pstrSeps;
if (!pstrSepsToUse)
{
pstrSepsToUse=pstrDefaultS
}
pstrToken = strtok( pstrSource, pstrSepsToUse );
while( pstrToken != NULL )
{
pstrDest[intNumTokens]=pst
intNumTokens++;
pstrToken = strtok( NULL, pstrSepsToUse );
}
return intNumTokens;
}
use this but split on space
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char string[] = "MyXStringXThatXIXWillXSpl it";
char **split_text(char *string,char *token, int *count)
{
char **retval = NULL;
char *ptr = NULL;
*count = 0;
ptr = strtok(string,token);
while(ptr != NULL)
{
*count = *count + 1;
retval = (char**)realloc(retval,(*c ount * sizeof(char*)));
retval[*count - 1] = strdup(ptr);
ptr = strtok(NULL,token);
}
return retval;
}
void main( void )
{
int count = 0;
char **tokens = split_text(string,"X",&cou nt);
for(int i=0;i<count;i++)
{
printf("%i ] %s\n",i,tokens[i]);
}
}
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char string[] = "MyXStringXThatXIXWillXSpl
char **split_text(char *string,char *token, int *count)
{
char **retval = NULL;
char *ptr = NULL;
*count = 0;
ptr = strtok(string,token);
while(ptr != NULL)
{
*count = *count + 1;
retval = (char**)realloc(retval,(*c
retval[*count - 1] = strdup(ptr);
ptr = strtok(NULL,token);
}
return retval;
}
void main( void )
{
int count = 0;
char **tokens = split_text(string,"X",&cou
for(int i=0;i<count;i++)
{
printf("%i ] %s\n",i,tokens[i]);
}
}
the disadvantage of strtok is that you do not get the matched (tokenized) character, it's just replaced by \0
Not a problem as long as you just have "+" ;-)
Not a problem as long as you just have "+" ;-)
1) pure numeric
2) pure alpha
3) everything else
or it could be more elaborate. Your question is too vague to give anything more than a generic answer as I have done.