break up string to multiple variables

I have a serial input.  the code below reads each byte and checks if its the start ($) or stop (#) bits.  Takes string of chars and puts it in a variable.
I actually need to get 2 variables out of this and I cant figure out how to split it.
so if my input is $abc|def#
I currently would get string=abc|def

I want:
string=abc
string2=def

char string[66];
int curWritePos;
int availableBytes = Serial.available();
    if (availableBytes > 0)
    {
        for(int i=0; i<availableBytes; i++) 
        {
           int ch = Serial.read();
           switch(ch)
           {
               case '#':
                 //  Serial.println(string);
                   displaycrap(); 
                   curWritePos=0;
                   string[curWritePos]=0;
                   break;
               case '$':
                   curWritePos=0;
                   string[curWritePos]=0;
                   break;
                      
               default:
                   if (curWritePos < sizeof(string)-1)
                   {
                       string[curWritePos++]=ch;
                       string[curWritePos]=0;
                   }
                   else
                   {
               //        Serial.println("Overflow\n");
                   }
                   break;
           }
        }
    }

Open in new window

char string[66];
int curWritePos;
int availableBytes = Serial.available();
    if (availableBytes > 0)
    {
        for(int i=0; i<availableBytes; i++) 
        {
           int ch = Serial.read();
           switch(ch)
           {
               case '#':
                 //  Serial.println(string);
                   displaycrap(); 
                   curWritePos=0;
                   string[curWritePos]=0;
                   break;
               case '$':
                   curWritePos=0;
                   string[curWritePos]=0;
                   break;
                      
               default:
                   if (curWritePos < sizeof(string)-1)
                   {
                       string[curWritePos++]=ch;
                       string[curWritePos]=0;
                   }
                   else
                   {
               //        Serial.println("Overflow\n");
                   }
                   break;
           }
        }
    }

Open in new window

LVL 1
livegirlloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
You could use a 2d array of string. Just increment the index when you see a starting char, '$' and an delimiter char, '|', as follows:
#include <string.h>
#include <stdio.h>

int main() {
    char string[2][66];
    char input[] = "$abc|def#";
    int curWritePos;  
    int availableBytes = strlen(input); //Serial.available(); 
    int i;
    int id=-1;
    if (availableBytes > 0)  
    {  
        for(i=0; i<availableBytes; i++)   
        {  
            //int ch = Serial.read();
            int ch = input[i];
            switch(ch)  
            {  
            case '#':  
                //  Serial.println(string);  
                // displaycrap();   
                //@@curWritePos=0;  
                string[id][curWritePos]=0;  
                break;  
            case '$': 
            case '|': 
                ++id;
                curWritePos=0;  
                string[id][curWritePos]=0;  
                break;  

            default:  
                if (curWritePos < sizeof(string)-1)  
                {  
                    string[id][curWritePos++]=ch;  
                    string[id][curWritePos]=0;  
                }  
                else  
                {  
                    //        Serial.println("Overflow\n");  
                }  
                break;  
            }  
        }  
    }
    printf("string1 = %s   string2 = %s\n", string[0], string[1]);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
livegirlloveAuthor Commented:
perfect thanks
0
phoffricCommented:
Sure thing
0
subratabiswasCommented:
Find an equivalent program below:

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

void splitAndPrint(char inStr[]);
int main(int argc, char *argv[]) {
    char string[66];
    int curWritePos;
    int c;
    int charNo = 0;

    while (1) {
        c = fgetc(stdin);
        if (c == '$') {
            curWritePos = 0;
            while (1) {
                c = fgetc(stdin);
                if (c == '#') {
                    string[curWritePos] = '\0';
                    splitAndPrint(string);
                    break;
                } else {
                    string[curWritePos++] = c;
                }
            }
        }
    }
}
void splitAndPrint(char inStr[]) {
    char *delimPosition;
    delimPosition = strstr(inStr, "|");

    *delimPosition = '\0';
    printf("string = %s\nstring2 = %s\n", inStr, delimPosition + 1);
}


Output from the program below:
/home/sbiswas/c> ./test
$This|That#
string = This
string2 = That
$His|Hers#
string = His
string2 = Hers
$Now|Then#
string = Now
string2 = Then
$First|Last#
string = First
string2 = Last

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.