strings.c

/*
 * strings.c -- show various important string functions
 *
 * Matt Bishop, ECS 36A
 *
 * April 30, 2024
 *	-- original program
 */
#include <stdio.h>
#include <string.h>

/*
 * macro
 */
								/* pause until newline typed */
#define PAUSE { printf("\nType ENTER to proceed >"); (void) getchar(); putchar('\n'); }
#define MAXLEN	1024		/* length of arrays */

/* * tis should be in string.h like the manual says, but apparently it isn't
 * so we do it here to shut the compiler up
 */
char *strdup(char *);

/*
 * the program
 */
int main(int argc, char *argv[])
{
	char buf1[MAXLEN], buf2[MAXLEN], buf3[MAXLEN]; /* used to hold variations on strings */
	char *av;		/* pointer to argv[1] (for convenience) */
	int i;			/* used as temp storage for integer */
	char *n;		/* used as temp storage for char pointer */

	/* check there is 1 argument only */
	if (argc != 2){
		fprintf(stderr, "Usage: %s string\n", argv[0]);
		return(1);
	}
	av = argv[1];

	/* strlen */
	printf("Length of argument '%s': %ld\n", av, strlen(av));

	PAUSE;

	/* strcpy, strncpy */
	printf("Copying the string: strcpy(buf, \"%s\")\n", av);
	strcpy(buf1, av);
	printf("\targument '%s', copy '%s'\n", av, buf1);

	printf("Copying 1000 chars of the string: strncpy(buf, \"%s\", 1000)\n", av);
	strcpy(buf2, "xxxxxxxxxxxxxx");
	strncpy(buf2, av, 1000);
	printf("\targument '%s', copy '%s'\n", av, buf2);
	
	printf("Copying 2 chars of the string: strncpy(buf, \"%s\", 2)\n", av);
	strcpy(buf3, "xxxxxxxxxxxxxx");
	strncpy(buf3, av, 3);
	printf("\targument '%s', copy '%s'\n", av, buf3);

	PAUSE;

	/* strcat, strncat */
	printf("Concatening 2 copies of argument: strcat(buf1, \"%s\")\n", av);

	strcat(buf1, av);
	printf("\targument '%s', copy '%s'\n", av, buf1);

	printf("Appending 1000 chars of the string: strncat(buf, \"%s\", 1000)\n", av);
	strcpy(buf2, av);
	strncat(buf2, av, 1000);
	printf("\targument '%s', copy '%s'\n", av, buf2);
	
	printf("Appending 2 chars of the string: strncat(buf, \"%s\", 2)\n", av);
	strcpy(buf3, av);
	strncat(buf3, av, 3);
	printf("\targument '%s', copy '%s'\n", av, buf3);

	PAUSE;

	/* strcmp, strncmp */
	printf("Compare string 'gleep' with argument: ");
	if ((i = strcmp("gleep", av)) > 0)
		printf("'gleep' comes before '%s'\n", av);
	else if (i == 0)
		printf("'gleep' is the same as '%s'\n", av);
	else if (i < 0)
		printf("'gleep' comes after '%s;\n", av);

	printf("Compare the first 3 chars of string 'gleep' with argument:\n");
	if ((i = strncmp("gleep", av, 3)) > 0)
		printf("\t'gleep' comes before '%s'\n", av);
	else if (i == 0)
		printf("\t'gleep' is the same as '%s'\n", av);
	else if (i < 0)
		printf("\t'gleep' comes after '%s;\n", av);

	PAUSE;

	/* strchr, strrchr */
	if ((n = strchr(av, 'h')) == NULL)
		printf("'h' does not appear in '%s'\n", av);
	else
		printf("The chars after the first 'h' are '%s'\n", n);
	if ((n = strrchr(av, 'h')) == NULL)
		printf("'h' does not appear in '%s'\n", av);
	else
		printf("The chars after the last 'h' are '%s'\n", n);
		

	PAUSE;

	/* strstr */
	if ((n = strstr(av, "hello")) == NULL)
		printf("The string 'hello' doesn't appear in the argument\n");
	else
		printf("The chars after the string 'hello' are '%s'\n", n);

	PAUSE;

	/* strtok */
	printf("break things 'a/bbb///cc;xxx:yyy' up\n");
	printf("token separators are ':;' and '/'\n");
	printf("first time through, we give strtok the string\n");
	strcpy(buf1, "a/bbb///cc;xxx:yyy");
	n = strtok(buf1, ":;/");
	if (n == NULL)
		printf("Tokens :;/ are not in the string\n");
	else{
		i = 1;
		printf("Token %d: '%s'\n", i++, n);
		while((n = strtok(NULL, ":;/")) != NULL)
			printf("Token %d: '%s'\n", i++, n);
	}

	PAUSE;
	
	/* strdup */
	if ((n = strdup(av)) != NULL)
		printf("A duplicate of %s is %s\n", av, n);
	else
		printf("Tried to duplicate %s but ran out of room\n", av);

	/* all done */
	return(0);
}




UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh