#include #include #include #define X "xxxx" #define Y "yyyy" #define BIN "Ab\0C" #define NUM "12341234" #define LINE "0,sensed,81,2017-11-14 08:48:05.000,68.0,142,61" void printit(char *lead, char x[], int ct) { int i; if (lead != NULL) printf("%s ", lead); putchar('\"'); for (i = 0; i < ct; i++){ if (isprint(x[i])) putchar(x[i]); else printf("\\%03o", x[i]); } putchar('\"'); } int main(int argc, char *argv[]) { char x[100]; char y[100]; char *z1, *z2; int i; (void) strcpy(x, X); (void) strcpy(y, Y); printit("Initially, x =", x, strlen(x)); printit(" and y =", y, strlen(y)); printit(" and BIN =", BIN, 4); putchar('\n'); printf("--------------\n\n"); /* now we do a memcpy of BIN onto x */ (void) memcpy(x, BIN, 4); printit("After memcopy of BIN, x =", x, 4); putchar('\n'); /* and a strcpy onto y */ (void) strcpy(y, BIN); printit("After strcpy of BIN, y =", y, 4); putchar('\n'); printf("--------------\n\n"); /* now we compare stuff */ (void) strcpy(x, X); (void) strcpy(y, Y); printit("Resetting, x =", x, strlen(x)); printit(" and y =", y, strlen(y)); printit(" and BIN =", BIN, 4); putchar('\n'); printf("--------------\n\n"); /* do a strcpy -- we want the last letter to be the original */ (void) strcpy(y, BIN); printit("After strcpy of BIN, y =", y, 4); putchar('\n'); printf("strcmp(BIN, y) = %d\n", strcmp(BIN, y)); printf("strncmp(BIN, y, 4) = %d\n", strncmp(BIN, y, 4)); printf("memcmp(BIN, y, 4) = %d\n", memcmp(BIN, y, 4)); printf("--------------\n\n"); /* and now for memset */ memset(x, 'a', 3); printit("memset(x, 'a', 3) sets x =", x, 4); putchar('\n'); printf("--------------\n\n"); /* this may or may not work */ strcpy(x, NUM); strcpy(y, NUM); printit("For this, x =", x, 4); printit(" and y =", y, 4); putchar('\n'); memcpy(x, &x[1], 2); strncpy(y, &y[1], 2); printit("memcpy(x, &x[1], 2) sets x =", x, 4); putchar('\n'); printit("strncpy(y, &y[1], 2) sets y =", y, 4); putchar('\n'); printf("--------------\n\n"); /* now we look for characters */ (void) strcpy(x, NUM); (void) strcpy(y, NUM); printit("Resetting, x =", x, strlen(x)); printit(" and y =", y, strlen(y)); putchar('\n'); printf("Find the first and last ocurrence of a character\n"); z1 = strchr(x, '1'); printf("strchr(x, '1') = %s", z1 == NULL ? "NULL" : z1); if (z1 == NULL) putchar('\n'); else printf(" (that's index %d)\n", (int)(z1 - x)); z2 = strrchr(x, '1'); printf("strrchr(x, '1') = %s", z2 == NULL ? "NULL" : z2); if (z2 == NULL) putchar('\n'); else printf(" (that's index %d)\n", (int)(z2 - x)); printf("--------------\n\n"); /* now we look substrings */ printf("Find the first occurrance of a substring\n"); z1 = strstr(x, "34"); printf("strstr(x, \"34\") = %s", z1 == NULL ? "NULL" : z1); if (z1 == NULL) putchar('\n'); else printf(" (begins at index %d)\n", (int)(z1 - x)); printf("--------------\n\n"); /* now break a string into tokens */ (void) strcpy(x, LINE); printit("Resetting, x =", x, strlen(x)); putchar('\n'); printf("It's from a csv file ... grab each field\n"); i = 0; z1 = strtok(x, ","); while(z1 != NULL){ printf("Field %2d: \"%s\"\n", i, z1); i += 1; z1 = strtok(NULL, ","); } printf("Done!\n"); printf("--------------\n\n"); return(0); }