Declaring the
string functions in
C's standard library
- The declarations of
the string functions in
C's standard library are
contained in
header files:
- How to get
the manual page on
the string functions:
|
Samples of
string functions
- int strlen(char *s):
- returns the
length
(not including the
sentinel '\0')
of string s
|
- char *strcpy(char *s1,
char *s2):
- copy
string s2
into the string s1
-
strcpy(s1, s2):
returns
s1
|
- char *strcat(char *s1,
char *s2):
- append (= catenate)
string s2
after the string s1
-
(string
s2 is
unchanged)
-
strcat(s1, s2):
returns
s1
|
- int strcmp(char *s1,
char * s2):
compare the
strings
s1 and
s2
- returns
a value
< 0
if s1 is
lexicographically
smaller
than s2
- returns
a value
0
if s1 is
lexicographically
equal
to s2
- returns
a value
> 0
if s1 is
lexicographically
larger
than s2
|
|
I like to show you
how some of these
functions are
implemented using
pointer arithmetic....
The strlen( )
function
- The strlen(s)
function is
as follows:
int strlen(char *s)
{
int len = 0; // Initialize len = 0
while ( *s != '\0' ) // Not the sentinel
{
len++; // One more character
s++; // Advance to next char
}
return(len);
}
|
|
DEMO:
demo/C/set2/strlen1.c
The abbreviated version
strlen( )
function
DEMO:
demo/C/set2/strlen2.c
Background info:
the assignment expression
- Many of the
string functions make
use of the fact that
the
assignment operator
(=)
returns the
assigned value:
x = 3; // = returns 3 !!!
|
- Example:
int main(int argc, char *argv[])
{
int x;
printf(">>> %d\n", x=3 ); // Prints the return value 3
}
|
|
DEMO:
demo/C/set2/assign-op1.c
Background info:
copying an
array using
pointer arithmetic
Background info:
copying an
array using
pointer arithmetic
Background info:
copying an
array using
pointer arithmetic
Background info:
copying an
array using
pointer arithmetic
Background info:
copying an
array using
pointer arithmetic
Background info:
copying an
array using
pointer arithmetic
The strcpy( )
function
using array notation
- The strcpy(s1, s2)
function
written with
array notation is:
char *strcpy( char s1[], char s2[] )
{
int i = 0;
/* ----------------------------------
'\0' marks the end of the string !
---------------------------------- */
while ( s2[i] != '\0' ) // Not end of s2
{
s1[i] = s2[i]; // Copy current character
i++; // Move to next character
}
s1[i] = '\0'; // Mark the end of the s1 string
return(s1);
}
|
|
DEMO:
demo/C/set2/strcpy1.c
We can write a
version of
strcpy( ) using
pointer arithmetic
with the
previously discussed
technique
The strcpy( )
function
using pointer arithmetic
- The strcpy(s1, s2)
function
written with
pointer arithmetic is:
char *strcpy( char s1[], char s2[] )
{
char *retVal = s1; // Save s1 to return
/* ----------------------------------
'\0' marks the end of the string !
---------------------------------- */
while ( (*s1++ = *s2++) != '\0' ); // Not end of s2
// Copy current character
// and move to next character
// until we copied '\0'
// Note: '\0' has been copied !
return(retVal);
}
|
|
DEMO:
demo/C/set2/strcpy3.c
Advice:
do not go
too crazy with
pointer arithmetic...
the code is
hard to
read...
The strcmp( )
function
using array notation
- The strcmp(s)
function
written
with array notation is:
int strcmp( char s1[], char s2[] )
{
int i = 0;
/* ------------------------------------------------------
Find the first distinct character in s1 and s2
------------------------------------------------------- */
while ( s2[i] != '\0' && s1[i] != '\0' )
{
if ( s1[i] != s2[i] ) // s1 and s2 differ in char i
break; // exit while loop !
i++; // Otherwise, check next char
}
return s1[i] − s2[i]; // Returns < 0 if s1[i] < s2[i]
// Returns 0 if s1[i] = s2[i]
// Returns > 0 if s1[i] > s2[i]
}
|
|
DEMO:
demo/C/set2/strcmp1.c
The strcmp( )
function
using pointer arithmetic
- The strcmp(s)
function
written
with pointer arithmetic is:
int strcmp( char s1[], char s2[] )
{
int i = 0;
/* ------------------------------------------------------
Find the first distinct character in s1 and s2
------------------------------------------------------- */
while ( *s2 != '\0' && *s1 != '\0' )
{
if ( *s1 != *s2 ) // s1 and s2 differ in char i
break; // exit while loop !
s1++; s2++; // Otherwise, check next char
}
return *s1 − *s2 ; // Returns < 0 if *s1 < *s2
// Returns 0 if *s1 = *s2
// Returns > 0 if *s1 > *s2
}
|
|
Instead of
using a break,
we can add
(*s1 != *s2) == FALSE
to the while-condition
to exit the
while-loop
The strcmp( )
function
using pointer arithmetic
- Final version of
strcmp( ):
int strcmp( char s1[], char s2[] )
{
int i = 0;
/* ------------------------------------------------------
Find the first distinct character in s1 and s2
------------------------------------------------------- */
while ( *s2 != '\0' && *s1 != '\0' && *s1 == *s2 )
{
s1++; s2++; // Otherwise, check next char
}
return *s1 − *s2 ; // Returns < 0 if *s1 < *s2
// Returns 0 if *s1 = *s2
// Returns > 0 if *s1 > *s2
}
|
|
DEMO:
demo/C/set2/strcmp3.c
❮
❯