chartype.c

/*
 * program to demonstrate the ctype macros
 * print the type of each letter in the argument
 *
 
 * Matt Bishop, ECS 36A
 * April 12, 2024	original version
 */
#include <stdio.h>
#include <ctype.h>

/*
 * main routine
 */
int main(int argc, char *argv[])
{
	char *s;	/* used to walk down string */

	/*
	 * Be sure there's an argument
	 */
	if (argc == 1){
		fprintf(stderr, "Usage: %s string\n", argv[0]);
		return(1);
	}

	/*
	 * walk the argument, printing the types of chars as we go
	 */
	for(s = argv[1]; *s; s++){
		printf("The character \'%c\' (\\%03o) is:\n", *s, *s);
		if (isalnum(*s)) printf("\talphanumeric\n");
		if (isalpha(*s)) printf("\talphabetic\n");
		if (isupper(*s)) printf("\tupper case\n");
		if (islower(*s)) printf("\tlower case\n");
		if (isspace(*s)) printf("\twhite space\n");
		if (iscntrl(*s)) printf("\ta control character\n");
		if (isdigit(*s)) printf("\ta digit\n");
		if (isxdigit(*s)) printf("\ta hexadecimal digit\n");
		if (ispunct(*s)) printf("\tpunctuation\n");
	}

	/*
	 * 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