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;
}
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;
}
No comments:
Post a Comment