Anda di halaman 1dari 27

Characters & Strings

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.

Fundamentals of strings & characters

Characters fundamental building blocks of source programs

Every program is composed of characters

Character constant an int value represented as a character in single quotes.

z represents an int value of z \n represents an int value of newline

Fundamentals of strings & characters

String series of characters treated as a single unit

May include letters, digits, and various special characters Juan dela Cruz acertain String1234

String constants are written in double quotes


A string is an array of characters ending in the null character (\0)

Fundamentals of strings & characters

Strings are basically pointers

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

Strings are like arrays

How to declare a string?

char color[] = blue;

Creates a 5-element array color containing the characters b ,l, u, e, and \0

const char *colorPtr = blue;

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

How to declare a string?

The actual string length is actually the size of string 1.

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

A string can be assigned to an array using scanf() or cin

scanf(%s, myString);

Since a string is also a pointer, the & is not necessary.

cin>>myString;

Note: in this case, a string can only handle ONE WORD

Character handling library


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

String Conversion Functions


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

Standard Input/Output Library Functions

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

The String Manipulation Functions of the String handling library

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)

String manipulation functions


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.

Comparison functions of the string handling library

int strcmp(const char *s1, const char *s2)


Compares s1 with s2. The functions returns 0 if s1 is equal to s2; less than 0 if s1 is less than s2, greater than 0 if s1 is greater than s2. int strncmp(cons char *s1, const char *s2, size_t n) Compares up to n characters of the string s1 with the string s2. The functions returns 0 if s1 is equal to s2; less than 0 if s1 is less than s2, greater than 0 if s1 is greater than s2.

Search functions of the string handling library


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

Memory functions of the string handling library

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.

Other functions of the string handling library

char *strerror(int errornum)

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.

size_t strlen(const char *s)

Some Predefined C-String Manipulation Functions (without pointers)

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.

Some Predefined C-String Manipulation Functions (without the use of * operator)

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.

Some Predefined C-String Manipulation Functions (without pointers)

strlen(src_string) Returns an integer equal to the length of the src_string. (The null character, \n, is not included in the length.)

Some Predefined C-String Manipulation Functions (without pointers)

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.

Some Predefined C-String Manipulation Functions (without pointers)

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 !

Some Predefined C-String Manipulation Functions (without pointers)

strlwr(string)
Converts string into lowercase mode

strupr(string)
Converts string into uppercase mode

Machine problems for strings

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)

Characters & Strings


The End

Anda mungkin juga menyukai