Mr. Exander T. Barrios, MIM, MIT UIC- ITE Instructor AY 2010- 2011
Objectives
To use the functions of the character handling library (ctype) To use the string and character input/output functions of the standard input/output library (stdio) To use the conversion functions of the general utilities library (stdlib) To use the string processing functions of the string handling library (string) To appreciate the power of function libraries as a means of achieving software reusability
Outline
Fundamentals of strings and characters Character handling library String conversion functions Standard input/output library functions String manipulation functions of the string handling library Comparison functions of the string handling library Search functions of the string handling library Memory functions of the string handling library Other functions of the string handling library
Introduction
The functions that will be discussed here enable programs to process characters, strings, lines of text and blocks of memory This lesson also focuses on the techniques used to develop editors, word processors, page layout software, and other kinds of textprocessing software.
May include letters, digits, and various special characters Juan dela Cruz acertain String1234
A string is accessed via a pointer to the first character The value of the string is the address of its first character A string, like an array, is also a pointer to its first element
Creates a pointer variable colorPtr that points to the string blue somewhere in memory Very explicit declaration, but can be inconvenient for programmers
char color[] = { b, l, u, e, \0 };
char word[20]; only contains 19 characters. 1 character is reserved for the null character (\0) The null character should always be at the end of the character array for it to be considered a string.
Reading a string
scanf(%s, myString);
cin>>myString;
It contains several functions that perform useful tests and manipulations of character data. It is therefore necessary that when you use these functions, you have to include <ctype.h> The complete list of the functions present in this library (c/o Textbook) Samples are: int isdigit(int c) - returns true if c is a digit int isalpha(int c) returns true if c is a letter int tolower(int c) converts c to lowercase int toupper(int c) converts c to uppercase
These are the functions that are present in the general utilities library <stdlib.h>. These functions convert string of digits to integer and floating-point values.
For the complete list (pls. refer to your Textbook, C++, How to Program by Deitel & Deitel).
Samples are:
atof(string) converts string to double value atoi(string) converts string to int value atol(string) converts string to long value strtol() converts string to long strtod() converts string to double
These functions are present in the standard input/output library <stdio.h> specifically for manipulating character and string data. For the complete list (pls. refer to your textbook) Samples are: getchar() inputs the next character and returns it as an integer gets() inputs characters from the keyboard into the an array until a newline character is encountered putchar(int c) prints the character stored in c. sprintf() like printf, except the output is stored in an array instead of printing it on the screen
These functions are present in the string handling library <string.h> which are used for manipulating string data, comparing strings, searching strings for characters and other strings, tokenizing strings, and determining the length of the strings. For the lists of functions (pls. refer to your textbook, C++, How to program, by Deitel & Deitel)
char *strcpy(char *s1, const char *s2) Copies s2 into s1. The value of s1 is returned. char *strncpy(char *s1, const char *s2, size_t n) Copies at most n characters of string s2 into array s1. The value of s1 is returned. char *strcat(char *s1, const char *s2) Appends s2 to s1. The value of s1 is returned char *strncat(char *s1, const char *s2, size_t n) Appends at most n characters of string s2 to s1. The value of s1 is returned.
char *strchr(const char *s, int c) st occurrence of character c in string s. Locates the 1 size_t strcspn(const char *s1, const char *s2) Determines & returns the length of the initial segment of string s1 consisting of characters not contained in s2 char *strpbrk(const char *s1, const char *s2) st occurrence in s1 of any character in Locates the 1 s2. char *strtok(char *s1, const char *s2) Breaks s1 into tokens separated by characters contained in string s2
void *memcopy(void *s1, const void *s2, size_t n) Copies n characters from the object pointed to by s2 into the object pointed to by s1. A pointer to the resulting object is returned. int memcmp(const void *s1, const void *s2, size_t n) Compares the first n characters of the object pointed to by s1 & s2. void *memset(void *s, int c, size_t n) st n characters of the object Copies c into the 1 pointed to by s.
Maps errornum into a full text string in a system-dependent manner. A pointer to the string is returned. Determines the length of string s. The number of characters preceding the terminating null character is returned.
strcpy(target_string_var, src_string) This copies the C string value src_string into the C string variable target-string var. strncpy(target_string_var, src_string, limit) The same as the two-argument strcpy except that at most limit characters are copied.
strcat(target_string_var, src_string) Concatenates the C string value src-string onto the end of the C string in the C string variable target_string_var. strncat(target_string_var,src_string, limit) The same as the two-argument strcat except that at most limit characters are appended.
strlen(src_string) Returns an integer equal to the length of the src_string. (The null character, \n, is not included in the length.)
strcmp(string1, string2) Returns 0 is string1 and string2 are the same. Returns a value<0 if string1 is less than string2. Returns a value>0 if string1 is greater than string2 (that is, returns a nonzero value if string1 and string2 are different). The order is lexicographic.
strncmp(string1, string2, limit) The same as the two-argument strcat except that at most limit characters are compared. strrev(string) Reverses a string strtok() for research !
strlwr(string)
Converts string into lowercase mode
strupr(string)
Converts string into uppercase mode
Write a program that inputs 4 string that represent integers, converts the strings into integers, sums the values and prints the total of the 4 values. Tokenizing Strings (page 358, 8.15) Palindrome Pig Latin (page 358, 8.13)