livegirllove
asked on
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
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;
}
}
}
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;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sure thing
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
#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
ASKER