SlideShare a Scribd company logo
1 of 55
Download to read offline
C Characters and Strings
Chapter 8
Outline
1. Introduction
2. Fundamentals of strings and characters
3. Character handling library
4. String conversion functions
5. Standard input/output library functions
6. The string handling library functions
2
3
Introduction
n Introduce some standard library functions
– Easy string and character processing
– Programs can process characters, strings, lines of text, and blocks of memory
n These techniques used to develop text-processing software as:
– Word processors
– Editors
In this chapter you will learn:
 T
o use the functions of the character-handling library (ctype)
 T
o use the string-conversion functions of the general utilities library (stdlib)
 T
o use the string and character input/output functions of the standard input/
output library (stdio)
 T
o use the string-processing functions of the string handling library (string)
 The power of function libraries as a means of achieving software reusability
Outline
1. Introduction
2. Fundamentals of strings and characters
3. Character handling library
4. String conversion functions
5. Standard input/output library functions
6. The string handling library functions
4
5
Fundamentals of strings and characters
Characters
– Building blocks of programs
• Every program is a sequence of meaningfully grouped characters
–Character constant  A character in single quotes
– a character is an integer (ASCII values range 0–255)
• 'z' represents the integer value of z
Strings
– Series of characters treated as a single unit
• Can include letters, digits and special characters (*, /, $)
–String literal (string constant)  Written in double quotes
• "Hello"
– A string is an array of characters
• A string is accessed via a pointer to the first character
• The value of a string is the address of first character
6
Fundamentals of strings and characters
String definitions
– Define as a character array or a variable of type char *
char
char
char
color[]
color[]
= "blue";
= {‘b’, ‘l’, ‘u’, ‘e’, ‘0’};
*colorPtr = "blue";
– Remember that strings represented as character arrays end with '0'
• color has 5 elements
Inputting strings
– Use scanf
scanf("%s", word);
• Copies input into word[] until a space, tab, newline or end-of-file
indicator
• Do not need & (because a string is a pointer)
Remember to leave room in the array for '0'
Outline
1. Introduction
2. Fundamentals of strings and characters
3. Character handling library
4. String conversion functions
5. Standard input/output library functions
6. The string handling library functions
7
8
Character handling library <ctype.h>
Character handling library
– Includes functions to perform useful tests and manipulations of
character data
– Each function receives a character (an int) or EOF as an argument
• The following slide contains a table of all the functions in <ctype.h>
9
Character handling library <ctype.h>
10
Character handling library <ctype.h>
/* Using functions isdigit, isalpha, isalnum, and isxdigit */
#include <stdio.h>
#include < ctype.h>
int main()
{
printf( "%sn%s%sn%s%snn", "According to isdigit: ",
isdigit( '8' ) ? "8 is a " : "8 is not a ", "digit",
isdigit( '#' ) ? "# is a " : "# is not a ", "digit" );
printf( "%sn%s%sn%s%sn%s%sn%s%snn",
"According to isalpha:",
isalpha( 'A' ) ? "A is a " : "A is not a ", "letter",
isalpha( 'b' ) ? "b is a " : "b is not a ", "letter",
isalpha( '&' ) ? "& is a " : "& is not a ", "letter",
isalpha( '4' ) ? "4 is a " : "4 is not a ", "letter" );
According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
printf("According to isdigit:n");
if (isdigit( '8' ))
printf("8 is a digitn");
else
printf("8 is not a digitn");
printf("According to isdigit:n");
if (isdigit( '#' ))
printf("# is a digitn");
else
printf("# is not a digitn");
isdigit tests if a character is a
decimal digit
isalpha tests if a character is a letter
A is a letter
b is a letter
& is not a letter
4 is not a letter
Character handling library <ctype.h>
Character handling library <ctype.h>
Character handling library <ctype.h>
Character handling library <ctype.h>
Character handling library <ctype.h>
Outline
1. Introduction
2. Fundamentals of strings and characters
3. Character handling library
4. String conversion functions
5. Standard input/output library functions
6. The string handling library functions
16
17
String conversion functions <stdlib.h>
n Conversion functions
Convert strings of digits to integer and floating-point values
– In <stdlib.h> (general utilities library)
String conversion functions <stdlib.h>
String conversion functions <stdlib.h>
String conversion functions <stdlib.h>
String conversion functions <stdlib.h>
String conversion functions <stdlib.h>
String conversion functions <stdlib.h>
Outline
1. Introduction
2. Fundamentals of strings and characters
3. Character handling library
4. String conversion functions
5. Standard input/output library functions
6. The string handling library functions
24
25
Standard input/output library functions <stdio.h>
Functions in <stdio.h>
• Used to manipulate character and string data
Standard input/output library functions <stdio.h>
Standard input/output library functions <stdio.h>
Standard input/output library functions <stdio.h>
Standard input/output library functions <stdio.h>
Standard input/output library functions <stdio.h>
Outline
1. Introduction
2. Fundamentals of strings and characters
3. Character handling library
4. String conversion functions
5. Standard input/output library functions
6. The string handling library functions
31
32
The string handling library functions <string.h>
String handling library has functions to:
– Manipulate string data
– Search strings
– T
okenize strings
– Determine string length
String manipulation functions:
The string handling library functions <string.h>
The string handling library functions <string.h>
35
n Comparing strings
– Computer compares numeric ASCII codes of characters in string
int strcmp( const char *s1, const char *s2 );
– Compares string s1 to s2
– Returns a negative number if s1 < s2, zero if s1 == s2 or a positive
number if s1 > s2
int strncmp(
size_t n
const char *s1, const char
);
*s2,
– Compares up to n characters of string s1 to s2
– Returns values as above
Compaison functions:
The string handling library functions <string.h>
36
/* Using strcmp and strncmp */
#include <stdio.h>
#include <string.h>
int main()
{
const char *s1 = "Happy New Year"; /* initialize char pointer */
const char *s2 = "Happy New Year"; /* initialize char pointer */
const char *s3 = "Happy Holidays"; /* initialize char pointer */
printf("%s%sn%s%sn%s%snn%s%2dn%s%2dn%s%2dnn",
"s1 = ", s1, "s2 = ", s2, "s3 = ", s3,
"strcmp(s1, s2) = ",
"strcmp(s1, s3) = ",
"strcmp(s3, s1) = ",
strcmp( s1, s2 )
strcmp( s1, s3 )
strcmp( s3, s1 )
,
,
);
printf("%s%2dn%s%2dn%s%2dn",
"strncmp(s1, s3, 6) = ",
"strncmp(s1, s3, 7) = ",
"strncmp(s3, s1, 7) = ",
strncmp( s1, s3, 6 )
strncmp( s1, s3, 7 )
strncmp( s3, s1, 7 )
,
,
);
return 0; /* indicates successful termination */
} /* end main */
The string handling library functions <string.h>
strcmp compares
string s1 to string s2
strncmp compares the first 6
characters of string s1 to the first
6 characters of string s3
s1 = Happy
s2 = Happy
s3 = Happy
New Year
New Year
Holidays
s2) = 0
strcmp(s1,
strcmp(s1,
strcmp(s3,
s3) = 1
s1) = -1
strncmp(s1,
strncmp(s1,
strncmp(s3,
s3, 6) = 0
s3, 7) = 1
s1, 7) = -1
37
Search functions:
The string handling library functions <string.h>
38
The string handling library functions <string.h>
Search functions:
39
The string handling library functions <string.h>
Search functions:
const char *string = "This is a test"; /* initialize char pointer */
char character1 = 'a'; /* initialize character1 */
char character2 = 'z'; /* initialize character2 */
'z' was not found in "This is a test".
The string handling library functions <string.h>
The string handling library functions <string.h>
The string handling library functions <string.h>
The string handling library functions <string.h>
The string handling library functions <string.h>
45
/* Using strtok */
#include <stdio.h>
#include <string.h>
int main()
{
/* initialize array string */
char string[] = "This is a sentence with 7 tokens";
char *tokenPtr; /* create char pointer */
printf( "%sn%snn%sn",
"The string to be tokenized is:", string,
"The tokens are:" );
tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */
/* continue tokenizing sentence until tokenPtr becomes NULL */
while ( tokenPtr != NULL ) {
printf( "%sn", tokenPtr );
tokenPtr = strtok( NULL, " " ); /* get next token */
} /* end while */
return 0; /* indicates successful termination */
} /* end main */
The string handling library functions <string.h>
Search functions:
The string to be tokenized is:
This is a sentence with 7 tokens
The tokens are:
This
is
a
sentence
with
7
tokens
strtok “tokenizes” string by
breaking it into tokens at each space
Calling strtok again and passing it NULL
continues the tokenizing of the previous string
The function then finds the next
delimiting character in the string and
replaces it with a null ('0') character to
terminate the current token
46
Memory functions:
The string handling library functions <string.h>
n Memory Functions
– In <string.h>
– Manipulate, compare, and search blocks of memory
– Can manipulate any block of data
n Pointer parameters are void *
–A pointer to any data type can be assigned to void *, and vice versa
–void * cannot be dereferenced
• Each function receives a size argument specifying the number of
bytes (characters) to process
47
The string handling library functions <string.h>
Memory functions:
The string handling library functions <string.h>
The string handling library functions <string.h>
The string handling library functions <string.h>
The string handling library functions <string.h>
The string handling library functions <string.h>
53
n size_t strlen( const char *s );
– Returns the number of characters (before the terminating
null character) in string s
char *strerror( int errornum );
– Creates a system-dependent error message based on
errornum
– Returns a pointer to the string
n
The string handling library functions <string.h>
Other functions:
The string handling library functions <string.h>
The string handling library functions <string.h>

More Related Content

Similar to characters_strings.pdf

Similar to characters_strings.pdf (20)

The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
U4.ppt
U4.pptU4.ppt
U4.ppt
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
C
CC
C
 
Pointers
PointersPointers
Pointers
 
Team 1
Team 1Team 1
Team 1
 
Console I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptxConsole I/o & basics of array and strings.pptx
Console I/o & basics of array and strings.pptx
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Character Arrays and strings in c language
Character Arrays and strings in c languageCharacter Arrays and strings in c language
Character Arrays and strings in c language
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
 
Modern C++
Modern C++Modern C++
Modern C++
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
string in C
string in Cstring in C
string in C
 
C string
C stringC string
C string
 
C language
C languageC language
C language
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
 

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

characters_strings.pdf

  • 1. C Characters and Strings Chapter 8
  • 2. Outline 1. Introduction 2. Fundamentals of strings and characters 3. Character handling library 4. String conversion functions 5. Standard input/output library functions 6. The string handling library functions 2
  • 3. 3 Introduction n Introduce some standard library functions – Easy string and character processing – Programs can process characters, strings, lines of text, and blocks of memory n These techniques used to develop text-processing software as: – Word processors – Editors In this chapter you will learn:  T o use the functions of the character-handling library (ctype)  T o use the string-conversion functions of the general utilities library (stdlib)  T o use the string and character input/output functions of the standard input/ output library (stdio)  T o use the string-processing functions of the string handling library (string)  The power of function libraries as a means of achieving software reusability
  • 4. Outline 1. Introduction 2. Fundamentals of strings and characters 3. Character handling library 4. String conversion functions 5. Standard input/output library functions 6. The string handling library functions 4
  • 5. 5 Fundamentals of strings and characters Characters – Building blocks of programs • Every program is a sequence of meaningfully grouped characters –Character constant  A character in single quotes – a character is an integer (ASCII values range 0–255) • 'z' represents the integer value of z Strings – Series of characters treated as a single unit • Can include letters, digits and special characters (*, /, $) –String literal (string constant)  Written in double quotes • "Hello" – A string is an array of characters • A string is accessed via a pointer to the first character • The value of a string is the address of first character
  • 6. 6 Fundamentals of strings and characters String definitions – Define as a character array or a variable of type char * char char char color[] color[] = "blue"; = {‘b’, ‘l’, ‘u’, ‘e’, ‘0’}; *colorPtr = "blue"; – Remember that strings represented as character arrays end with '0' • color has 5 elements Inputting strings – Use scanf scanf("%s", word); • Copies input into word[] until a space, tab, newline or end-of-file indicator • Do not need & (because a string is a pointer) Remember to leave room in the array for '0'
  • 7. Outline 1. Introduction 2. Fundamentals of strings and characters 3. Character handling library 4. String conversion functions 5. Standard input/output library functions 6. The string handling library functions 7
  • 8. 8 Character handling library <ctype.h> Character handling library – Includes functions to perform useful tests and manipulations of character data – Each function receives a character (an int) or EOF as an argument • The following slide contains a table of all the functions in <ctype.h>
  • 10. 10 Character handling library <ctype.h> /* Using functions isdigit, isalpha, isalnum, and isxdigit */ #include <stdio.h> #include < ctype.h> int main() { printf( "%sn%s%sn%s%snn", "According to isdigit: ", isdigit( '8' ) ? "8 is a " : "8 is not a ", "digit", isdigit( '#' ) ? "# is a " : "# is not a ", "digit" ); printf( "%sn%s%sn%s%sn%s%sn%s%snn", "According to isalpha:", isalpha( 'A' ) ? "A is a " : "A is not a ", "letter", isalpha( 'b' ) ? "b is a " : "b is not a ", "letter", isalpha( '&' ) ? "& is a " : "& is not a ", "letter", isalpha( '4' ) ? "4 is a " : "4 is not a ", "letter" ); According to isdigit: 8 is a digit # is not a digit According to isalpha: printf("According to isdigit:n"); if (isdigit( '8' )) printf("8 is a digitn"); else printf("8 is not a digitn"); printf("According to isdigit:n"); if (isdigit( '#' )) printf("# is a digitn"); else printf("# is not a digitn"); isdigit tests if a character is a decimal digit isalpha tests if a character is a letter A is a letter b is a letter & is not a letter 4 is not a letter
  • 16. Outline 1. Introduction 2. Fundamentals of strings and characters 3. Character handling library 4. String conversion functions 5. Standard input/output library functions 6. The string handling library functions 16
  • 17. 17 String conversion functions <stdlib.h> n Conversion functions Convert strings of digits to integer and floating-point values – In <stdlib.h> (general utilities library)
  • 24. Outline 1. Introduction 2. Fundamentals of strings and characters 3. Character handling library 4. String conversion functions 5. Standard input/output library functions 6. The string handling library functions 24
  • 25. 25 Standard input/output library functions <stdio.h> Functions in <stdio.h> • Used to manipulate character and string data
  • 26. Standard input/output library functions <stdio.h>
  • 27. Standard input/output library functions <stdio.h>
  • 28. Standard input/output library functions <stdio.h>
  • 29. Standard input/output library functions <stdio.h>
  • 30. Standard input/output library functions <stdio.h>
  • 31. Outline 1. Introduction 2. Fundamentals of strings and characters 3. Character handling library 4. String conversion functions 5. Standard input/output library functions 6. The string handling library functions 31
  • 32. 32 The string handling library functions <string.h> String handling library has functions to: – Manipulate string data – Search strings – T okenize strings – Determine string length String manipulation functions:
  • 33. The string handling library functions <string.h>
  • 34. The string handling library functions <string.h>
  • 35. 35 n Comparing strings – Computer compares numeric ASCII codes of characters in string int strcmp( const char *s1, const char *s2 ); – Compares string s1 to s2 – Returns a negative number if s1 < s2, zero if s1 == s2 or a positive number if s1 > s2 int strncmp( size_t n const char *s1, const char ); *s2, – Compares up to n characters of string s1 to s2 – Returns values as above Compaison functions: The string handling library functions <string.h>
  • 36. 36 /* Using strcmp and strncmp */ #include <stdio.h> #include <string.h> int main() { const char *s1 = "Happy New Year"; /* initialize char pointer */ const char *s2 = "Happy New Year"; /* initialize char pointer */ const char *s3 = "Happy Holidays"; /* initialize char pointer */ printf("%s%sn%s%sn%s%snn%s%2dn%s%2dn%s%2dnn", "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, "strcmp(s1, s2) = ", "strcmp(s1, s3) = ", "strcmp(s3, s1) = ", strcmp( s1, s2 ) strcmp( s1, s3 ) strcmp( s3, s1 ) , , ); printf("%s%2dn%s%2dn%s%2dn", "strncmp(s1, s3, 6) = ", "strncmp(s1, s3, 7) = ", "strncmp(s3, s1, 7) = ", strncmp( s1, s3, 6 ) strncmp( s1, s3, 7 ) strncmp( s3, s1, 7 ) , , ); return 0; /* indicates successful termination */ } /* end main */ The string handling library functions <string.h> strcmp compares string s1 to string s2 strncmp compares the first 6 characters of string s1 to the first 6 characters of string s3 s1 = Happy s2 = Happy s3 = Happy New Year New Year Holidays s2) = 0 strcmp(s1, strcmp(s1, strcmp(s3, s3) = 1 s1) = -1 strncmp(s1, strncmp(s1, strncmp(s3, s3, 6) = 0 s3, 7) = 1 s1, 7) = -1
  • 37. 37 Search functions: The string handling library functions <string.h>
  • 38. 38 The string handling library functions <string.h> Search functions:
  • 39. 39 The string handling library functions <string.h> Search functions: const char *string = "This is a test"; /* initialize char pointer */ char character1 = 'a'; /* initialize character1 */ char character2 = 'z'; /* initialize character2 */ 'z' was not found in "This is a test".
  • 40. The string handling library functions <string.h>
  • 41. The string handling library functions <string.h>
  • 42. The string handling library functions <string.h>
  • 43. The string handling library functions <string.h>
  • 44. The string handling library functions <string.h>
  • 45. 45 /* Using strtok */ #include <stdio.h> #include <string.h> int main() { /* initialize array string */ char string[] = "This is a sentence with 7 tokens"; char *tokenPtr; /* create char pointer */ printf( "%sn%snn%sn", "The string to be tokenized is:", string, "The tokens are:" ); tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */ /* continue tokenizing sentence until tokenPtr becomes NULL */ while ( tokenPtr != NULL ) { printf( "%sn", tokenPtr ); tokenPtr = strtok( NULL, " " ); /* get next token */ } /* end while */ return 0; /* indicates successful termination */ } /* end main */ The string handling library functions <string.h> Search functions: The string to be tokenized is: This is a sentence with 7 tokens The tokens are: This is a sentence with 7 tokens strtok “tokenizes” string by breaking it into tokens at each space Calling strtok again and passing it NULL continues the tokenizing of the previous string The function then finds the next delimiting character in the string and replaces it with a null ('0') character to terminate the current token
  • 46. 46 Memory functions: The string handling library functions <string.h> n Memory Functions – In <string.h> – Manipulate, compare, and search blocks of memory – Can manipulate any block of data n Pointer parameters are void * –A pointer to any data type can be assigned to void *, and vice versa –void * cannot be dereferenced • Each function receives a size argument specifying the number of bytes (characters) to process
  • 47. 47 The string handling library functions <string.h> Memory functions:
  • 48. The string handling library functions <string.h>
  • 49. The string handling library functions <string.h>
  • 50. The string handling library functions <string.h>
  • 51. The string handling library functions <string.h>
  • 52. The string handling library functions <string.h>
  • 53. 53 n size_t strlen( const char *s ); – Returns the number of characters (before the terminating null character) in string s char *strerror( int errornum ); – Creates a system-dependent error message based on errornum – Returns a pointer to the string n The string handling library functions <string.h> Other functions:
  • 54. The string handling library functions <string.h>
  • 55. The string handling library functions <string.h>