LAB 8: CHARACTER AND STRING Exercise 1: Character manipulation functions The following program demonstrates the usage of (predefined) character manipulation functions. In order to use the following functions, you need to include ctype.h as one of your header files. Copy and paste the following program: char ch; printf("enter a character, any character including symbol: "); scanf("%c",&ch); /*copy paste the following code here, one by one and observe the result*/ Copy and paste the following character manipulation function and observe the result. a) /* this function returns true if ch is a lowercase letter */ if(islower(ch)) printf("%c is a lowercase letter\n", ch); b) /* this function returns true if ch is an uppercase letter */ if(isupper(ch)) printf("%c is a uppercase letter\n", ch); c) /* this function returns true if ch is an alphabet */ if(isalpha(ch)) printf("%c is an alphabetic character\n", ch); d) /* this function returns true if ch is a digit */ if(isdigit(ch)) printf("%c is a decimal digit\n", ch); e) /* this function returns true if ch is a digit OR a letter */ if(isalnum(ch)) printf("%c is a alphanumeric character\n", ch); f) /* this function returns true if ch is a hexadecimal digit */ if(isxdigit(ch)) printf("%c is a hexadecimal digit\n", ch); g) /* this function returns true if ch is a printing character */ /* other than space, a digit or a letter */ if(ispunct(ch)) printf("%c is a punctuation character\n", ch); 1
h) /* this function returns true if ch is a whitespace character */ /* newline, space (' '), carriage return, tab */ if(isspace(ch)) printf("%c is a whitespace character\n", ch); i) /* this function returns true if ch is a printing character */ /* other than space */ if(isgraph(ch)) printf("%c is a printing character other than space\n", ch); j) /* this function returns true if ch is a printing character */ /* including space */ if(isprint(ch)) printf("%c is a printing character including space\n", ch); k) /* this function returns true if ch is a control character */ if(iscntrl(ch)) printf("%c is a control character\n", ch); Exercise 2: Character conversion function tolower( ) and toupper( ) are two functions that we use to change the letter case. tolower( ) is used to change an uppercase letter to its lowercase representation. toupper( ) is used to change a lowercase letter to its uppercase representation. You need to include ctype.h as one of your header files. Part 1: conversion function Copy and paste the following program. Run the program with different input (upper or lowercase letter) and observe the result. char ch; printf("enter a character: "); scanf("%c",&ch); if(islower(ch)) printf("%c in its uppercase letter is %c\n",ch, toupper(ch)); else if (isupper(ch)) printf("%c in its lowercase letter is %c\n",ch, tolower(ch)); else printf("neither an uppercase letter or lowercase letter entered\n"); 2
Part 2: The practical usage of tolower and toupper Both tolower( ) and toupper( ) functions are particularly useful during input validation. Say for a program that would keep on looping as long as the users type in y (for yes). We could use tolower( ) function to force any uppercase letter Y being entered by the users so that the program will still run correctly eventhough the users type in uppercase letter instead of lowercase letter. char cont = 'y'; while (cont == 'y') printf("\nkeep on looping"); fflush(stdin); printf("continue? (y/n):"); cont = tolower(getchar()); /* convert the read input to lowercase */ /* and assign it to variable cont */ Exercise 3: String manipulation functions In order for you to use string manipulation functions, you need to include string.h as one of your header files. Copy the following string declaration: #include <string.h> char str1[50] = "Malaysia Gemilang"; char str2[ ] = "Cemerlang"; char str3[100] = '\0'; int count; char str4[100] ='\0'; /* copy and paste the statements here */ 3
Copy and paste the following string manipulation function and observe the result. a) strcpy(str1, str2); /* copy the content of str2 into str1 */ b) strncpy(str1, str2, 4); /* copy the first 4 characters of str2 to str1 */ c) strcat(str1, str2); /* append str2 to str1 */ d) puts(str3); strncat(str1, str2, 4); /* append the 1 st 4 characters of str2 to str1 */ e) count = strlen(str1); printf("\n%s has %d characters including the space\n", str1, count); f) printf("\n\nenter the same string as above:"); gets(str4); if (strcmp(str1, str4) == 0) printf("both string are equal\n"); else printf("both string are not equal\n"); EXERCISE 4 Write a program that will count the number of alphabets, digits and punctuations in a sentence given by the user. (Hint: Use isalpha(), isdigit(), ispunct() and a repetition structure.) Enter your sentence: my tel no is 12345678. Your sentence contains: Alphabets: 9 Digits: 8 Punctuations: 1 4
EXERCISE 5 Write a program that prompts the user to enter his/her first name and a phone number. Your function should construct a user ID code by taking the first letter of the first name, followed by the first three digits of the phone number. Display the user ID code. (Hint: Use strncpy() and strncat().) Enter your first name and a phone number: zurin 3137862 Your user ID code is z313. 5