/*
* show the difference between the str* functions and the mem* functions
*
* Matt Bishop, ECS 36A
* -- ??? original program
* -- May 22, 2024 commented and cleaned up
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*
* macros
*/
/* next four are strings used to demo str*, mem* */
#define X "xxxx"
#define Y "yyyy"
#define BIN "Ab\0C"
#define NUM "12341234"
/* used to demo strtok */
#define LINE "0,sensed,81,2017-11-14 08:48:05.000,68.0,142,61"
/*
* weird print function
* first argument is printed normally
* for the second, this prints the first ct chars of x are printed
* so you can see them all
*/
void printit(char *lead, char x[], int ct)
{
int i; /* counter in a for loop */
/* print the leading part */
/* this is a normal string */
if (lead != NULL)
printf("%s ", lead);
/* delimit the string to be printed character by character */
putchar('\"');
/* print the chars in the string, either in */
/* their normal form, or as an octal escape */
for (i = 0; i < ct; i++){
if (isprint(x[i]))
putchar(x[i]);
else
printf("\\%03o", x[i]);
}
/* indicate the end */
putchar('\"');
}
/*
* the big cheese
*/
int main(void)
{
char x[100]; /* buffer for the mem* functions */
char y[100]; /* buffer for the str* functions */
char *z1, *z2; /* char pointer for results of functions */
int i; /* counter in a for loop */
/*
* the setup; report what was done
*/
(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 reset things
*/
(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");
/*
* now we do a comparison
*/
(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 demonstrate 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
* the problem is we're copying an array into itself, offset by 4
*/
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");
/*
* reset everything
*/
(void) strcpy(x, NUM);
(void) strcpy(y, NUM);
printit("Resetting, x =", x, strlen(x));
printit(" and y =", y, strlen(y));
putchar('\n');
/*
* `now we look for characters
*/
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");
/* finissimo! */
return(0);
}
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |