1 / 11

Introduction to C Programming CE00312-1

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.

makala
Download Presentation

Introduction to C Programming CE00312-1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to C ProgrammingCE00312-1 Lecture 17 Pointers and Strings

  2. 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.

  3. 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 }

  4. 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; }

  5. 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));

  6. 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:

  7. 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 }

  8. 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 *);

  9. 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

  10. { 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.

  11. { 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

More Related