简体   繁体   中英

cut characters from a string in C

I have a circuit that accepts data (an array of characters to be specific) via bluetooth from my android program.

now, what I want to do is: reprogram my microcontroller, I wish to filter or extract a series of characters from the array that my micrcontroller receives.

My android passes these three kinds of char arrays:

1. red#ILOVEYOU
2. blue#ILOVEYOULALALA
3. green#ILOVEDIANAROSECUSTODIO:)

I want to extract the red# or blue# or green# and transfer it to other variable. my phone sends a '#' in the end of each colors because I guess it will make the extraction easier.

char Comp(char* This){

    while(Serial.available() > 0) // Don't read unless
    // there you know there is data
    {
        if(index < 99){
            inChar = Serial.read(); // Read a character
            if(inChar == '#'){

            }
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string

        }
    }

    if(strcmp(inData,This)  == 0){
        for(int i=0;i<19;i++){
            inData[i]=0;
        }
        index=0;
        return(0);

    }
    else{
        return(1);
    }
}

This is my code in arduino. You see, it receives one character at a certain very little time and then adds it to a char array variable.

您可以在c中使用strtok()函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM