UNIT 03 - Lecture 30 - Manipulating String
UNIT 03 - Lecture 30 - Manipulating String
Semester : II
SKASC 1
Topic to be Discussed
Manipulating Strings
String Manipulation Functions
3
String Manipulation
char *strcpy( char *s1, const char *s2 Copies the string s2 into the character
); array s1. The value of s1 is returned.
char *strncpy( char *s1, const char Copies at most n characters of the string s2 into the
*s2, size_t n ); character array s1. The value of s1 is returned.
char *strcat( char *s1, const char *s2 Appends the string s2 to the string s1. The first character
); of s2 overwrites the terminating null character of s1. The
value of s1 is returned.
char *strncat( char *s1, const char Appends at most n characters of string s2 to string s1.
*s2, size_t n ); The first character of s2 overwrites the terminating null
character of s1. The value of s1 is returned.
int strcmp( const char *s1, const char Compares the string s1 with the string s2. The function
*s2 ); returns a value of zero, less than zero or greater than zero
if s1 is equal to, less than or greater than s2, respectively.
4
int strncmp( const char *s1, const char Compares up to n characters of the string s1 with the
*s2, size_t n ); string s2. The function returns zero, less than zero or
greater than zero if s1 is equal to, less than or greater
than s2, respectively.
char *strtok( char *s1, const char *s2 ); A sequence of calls to strtok breaks string s1 into
“tokens”—logical pieces such as words in a line of
text—delimited by characters contained in string s2.
The first call contains s1 as the first argument, and
subsequent calls to continue tokenizing the same
string contain NULL as the first argument. A pointer to
the current token is returned by each call. If there are
no more tokens when the function is called, NULL is
returned.
size_t strlen( const char *s ); Determines the length of string s. The number of
characters preceding the terminating null character is
returned.
5
String Manipulation Functions
• Copying strings
6
<cstring> contains
prototypes for strcpy
and strncpy.
8
<cstring> contains
prototypes for strcat
and strncat.
Append s2 to s1.
9
String Manipulation Functions
• Comparing strings
• Characters represented as numeric codes
• Strings compared using numeric codes
• Character codes / character sets
• ASCII
• “American Standard Code for Information
Interchage”
• EBCDIC
• “Extended Binary Coded Decimal Interchange Code”
10
String Manipulation Functions
• Comparing strings
• int strcmp( const char *s1, const char *s2 )
• Compares character by character
• Returns
• Zero if strings equal
• Negative value if first string less than second string
• Positive value if first string greater than second string
• int strncmp( const char *s1,
const char *s2, size_t n )
• Compares up to specified number of characters
• Stops comparing if reaches null character in one of
arguments
11
<cstring> contains
prototypes for strcmp
and strncmp.
12
String Manipulation Functions
• Tokenizing
• Breaking strings into tokens, separated by delimiting characters
• Tokens usually logical units, such as words (separated by
spaces)
• "This is my string" has 4 word tokens (separated by
spaces)
• char *strtok( char *s1, const char *s2 )
• Multiple calls required
• First call contains two arguments, string to be tokenized
and string containing delimiting characters
• Finds next delimiting character and replaces with null
character
• Subsequent calls continue tokenizing
• Call with first argument NULL 13
String Manipulation Functions
14
Thank You