Chitika

Wednesday, December 7, 2011

Shift characters left in string

Recently I have to received data from serial port, but data was asynchronous, so I have to match the sentinal value in the string. I read a buffer then I keep reading one character at a time and shifting string left one character and searching string to find the sentinal value, following is the function to shift characters left:


char *shiftLeft(char *str, int count) {

        char *savPtr=str;
        int len = strlen(str);

        while(*str) {

                if(str+count <= savPtr + len) {
                        *str = *(str+count);
                        str++;
                } else {
                        *str = 0;
                }
        }

        return savPtr;
}