Solved

Splite string into varaibles.

Posted on 2002-05-05
12
259 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:superleo
  • 2
  • 2
  • 2
  • +5
12 Comments
 

Expert Comment

by:HobbitHouse
ID: 6990706
you need to define your parsing rules and write the appropriate code.  Scan the string char by char and every time you hit a "type" boundary (which you define) start saving into a new array element.  Types could be

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.

0
 

Author Comment

by:superleo
ID: 6990934
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"??
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 6991294
In the specific case you are siting, you could determine "type" by calling isdigit. Read the string character by character. Check the type with isdigit. As long as the type remains the same, add the character to a receiving string. When the type changes you may need to, if the old type was numeric (isdigit returns true) you would need to convert the receiving string into a number. Depending on the exact nature of your input, you may also need to decide how to treat blanks etc. etc. In psuedo code it would look something like:

char receive[100];
char op[5];
int n1,n2,lasttype;

set receive[0] to first character
set lasttype to isdigit for first character
while characters left
   check if "current" character is same type as last
   if it is
      add "current" character to receiving string
   else
      if lasttype was string
         copy string in receive to op
      else
         convert string in receive to n1 or n2
0
 
LVL 16

Expert Comment

by:imladris
ID: 6991296
Oh, and one more line at the end of the while:

    set lasttype to type of "current" character
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6991304
2 superleo
int atoi( const char *string );  ??
>...by using "type"??
what do you mean?

0
 
LVL 4

Expert Comment

by:pagladasu
ID: 6991513
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);

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 6991969
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.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6992296
Use strtok()
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6992304
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=pstrDefaultSeps;
          }

     pstrToken = strtok( pstrSource, pstrSepsToUse );
     while( pstrToken != NULL )
          {
          pstrDest[intNumTokens]=pstrToken;
          intNumTokens++;
     pstrToken = strtok( NULL, pstrSepsToUse );
          }

     return     intNumTokens;
}
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6992380
0
 
LVL 10

Expert Comment

by:makerp
ID: 6993203
use this but split on space

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char string[] = "MyXStringXThatXIXWillXSplit";

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,(*count * 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",&count);
     for(int i=0;i<count;i++)
     {
          printf("%i ] %s\n",i,tokens[i]);
     }
}
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6993457
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 "+" ;-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now