110 likes | 183 Views
Introduction to C Programming CE00312-1. Lecture 17 Pointers and Strings. String function example. The function, string_tolower, converts all upper case letters in a string to lower case and leaves all other characters unchanged.
E N D
Introduction to C ProgrammingCE00312-1 Lecture 17 Pointers and Strings
String function example • The function, string_tolower, converts all upper case letters in a string to lower case and leaves all other characters unchanged. • It uses a standard function, tolower, to convert each character. • A pointer will examine each character one at a time while it has not yet reached the end of the string, i.e. ‘\0’. • A pointer to the beginning of the string is returned.
Function string_tolower char * string_tolower(char *s)//s is string { // return p as a pointer char *p; p = s; // start of string, s while ( *s != ‘\0’) // not end of string { *s = tolower(*s); // upper to lower s++; // next character } return p; // start of result }
Main function #include “stdio.h” #include “ctype.h” // for tolower function int main(void) { char line[81] ; // for string storage gets(line); // string of upper & lower case letters string_tolower(line); printf(“\nresulting string is:\n”); printf(“%s\n”, line); return 0; }
The function, string_tower, also returns a pointer to the result – so that it could be used as follows: printf(“\nresulting string is:\n%s”, string_tolower(line));
Another string function example • Function, isinteger, checks whether all the characters in a string are digits or not. • It returns true, i.e. 1, if all characters are the digits ‘0’ to ‘9’, otherwise it returns false, i.e. 0. • It uses a standard function, isdigit, to check each character:
Function isinteger int isinteger(char *s) // s is a string { // returns 1(true) or 0(false) while (*s != ‘\0’) { // while not end of string if (!isdigit(*s)) // not a digit { return 0; // false } s++; // next character } return 1; // true }
Validate Room Number Example Here is a program, using our functions, that reads and validates a room number for our computing labs. Room numbers range from KC01 to KC16 inclusive. Note that KC1 and KC01 are the same and that the letter prefixes, ‘K’ and ‘C’ may be in upper or lower case. #include “stdio.h” #include “string.h” #include “ctype.h” // isdigit, atoi, tolower // prototypes int isinteger(char *); char *string_tolower(char *);
int main(void) { char room[10]; int room_num; printf(“type in room number\n”); scanf(“%s”, room); // read as a string if (strlen(room) < 3) // too short { printf(“\n%s too short\n”, room); } else if (strlen(room) > 4) // too long { printf(“\n%s too long\n”, room); } else // length is ok
{ string_tolower(room);// lower case if ((room[0] != ‘k’) // not k or || (room[1] != ‘c’)) // not c { printf(“\nroom must start kc\n”); } else // kc or KC found if (!isinteger(room + 2)) // integer starting room[2]? { printf(“\nroom number not digits”); } else // is integer room no.
{ room_num = atoi(room + 2); // convert to integer if (room_num == 0||room_num >16) { printf(“\nroom %s ”, room); printf(“not a Concourse lab”); } else // out of range { printf(“\nroom %s ”, room); printf(“is a Concourse lab”); } } // end integer room number } // end length ok return 0; } // end main