Você está na página 1de 12

Fundamentals of Strings and Characters

Character-Handling Library

Computer Programming

Dr. Osman Parlaktuna


Dr. Burak Kaleci

March 2, 2017

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Content

Fundamentals of Strings and Characters

Character-Handling Library

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Characters

I Characters are the fundamental building blocks of source


programs.
I A program may contain character constants.
I A character constant is an int value represented as a
character in single quotes.
I For example, z represents the integer value of z (122), and
\n the integer value of newline (10) in ASCII.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

ASCII Table

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

An Example Program for Characters

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Strings

I A string is a series of characters (i.e. letters, digits and


various special characters such as +, -, *, / and $.) treated as
a single unit.
I It is clear that a string in C is an array of characters which
ends with the null character (\0).
I A string is accessed via a pointer to the first character in the
string.
I Thus, in C, it is appropriate to say that a string is a pointer to
the first character of the string.
I String literals, or string constants, in C are written in double
quotation marks.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Definition and Initialization of Strings


I A character array can be initialized with a string in a
definition as follows:
char color[] = { b, l, u, e, \0 }; or
char color[] = blue;
I The definitions create a 5-element array color containing the
characters b, l, u, e and \0.
I When defining a character array , it must be large enough to
store the string and its terminating null character.
I The preceding definitions automatically determine the size of
the array based on the initializer list.
I Also, a character pointer can be initialized with a string in a
definition as follows:
const char *colorPtr = blue;
I The definition creates pointer variable colorPtr that points to
the string blue somewhere in memory.
Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming
Fundamentals of Strings and Characters
Character-Handling Library

Strings with scanf


I Consider we have a character array such as char word[20]
I A string can be stored in an array using scanf as follows:
scanf( %s, word );
I Variable word is an array, which is, of course, a pointer, so the
& is not needed with argument word.
I scanf will read characters until a space, tab, newline or
end-of-file indicator is encountered. So, it is possible that the
user input could exceed 19 characters and that your program
might crash!
I To avoid this situation use conversion specifier %19s
scanf( %19s, word );
I In this case, scanf reads up to 19 characters and saves the last
character for the terminating null character.
I For a character array to be printed as a string, the array must
contain a terminating null character.
Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming
Fundamentals of Strings and Characters
Character-Handling Library

Character-Handling Library

The character-handling library (ctype.h) includes several


functions that perform useful tests and manipulations of character
data.
I int isdigit( int c ); Returns a true value if cis a digitand 0
(false) otherwise.
I int isalpha( int c ); Returns a true value if cis a letterand 0
otherwise.
I int isalnum( int c ); Returns a true value if cis a digitor a
letterand 0 otherwise.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Digits and Alphas

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Lowercase and Uppercase

I int islower( int c ); Returns a true value if c is a lowercase


letter and 0 otherwise.
I int isupper( int c ); Returns a true value if c is an uppercase
letter and 0 otherwise.
I int tolower( int c ); If c is an uppercase letter, tolower returns
c as a lowercase letter. Otherwise, tolower returns the
argument unchanged.
I int toupper( int c ); If c is a lowercase letter, toupper returns
c as an uppercase letter. Otherwise, touppe rreturns the
argument unchanged.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Fundamentals of Strings and Characters
Character-Handling Library

Lowercase and Uppercase

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming

Você também pode gostar