Strings
Topics
• String Libraries
• String Operations
• Sample Program
Reading
• Sections 8.1 - 8.7
CMSC 104, Version 8/06 L25Strings.ppt 1
String
• A string is a character array that has a marker to
show where the data ends, when the array is
larger than the data.
char firstname[ 50 ] = “Sue”;
• The array firstname is 50 characters long, but the
data is only four characters long. You must count
the marker.
• The marker is character that is set to the number
zero. (Sometimes called a null terminator.)
CMSC 104, Version 8/06 L25Strings.ppt 2
Array Constraint
• There are now built-in operators to
manipulate arrays, except to initialize them
when you declare the array:
char firstname[ 50 ] = “Sue”;
CMSC 104, Version 8/06 L25Strings.ppt 3
Missing Operators
• There is no string assignment operators.
• There are not string comparison operators.
• There are not string combination operators.
• However, there are built-in functions to do
this common tasks.
CMSC 104, Version 8/06 L25Strings.ppt 4
Built-in String Functions
• String assignment:
strcpy( destination, source )
char name[ 25 ]; /* contains nothing */
strcpy( name, “Hilton” ); /* name now
contains “Hilton” */
CMSC 104, Version 8/06 L25Strings.ppt 5
Built-in String Functions (cont‟d)
• String comparison:
strcmp( strA, strB );
o If strA comes after strB, the function returns a positive
number.
o Is strB comes last, the function returns a negative
number.
o If strA and strB are the same thing, the function returns
a zero.
result = strcmp( “CMSC”, “IFSM” ); /* negative */
result = strcmp( “IFSM”, “CMSC” ); /* positive */
result = strcmp( “CSMC”, “CMSC” ); /* zero */
CMSC 104, Version 8/06 L25Strings.ppt 6
Built-in String Functions (cont‟d)
• String combination:
strcat( destination, source )
o The source is not changed.
o The destination contain exactly what it had before plus
what was in the source. Nothing else is added. NOTE:
If you are combining a first name and last name for a
full name, you must use another strcat to add the space
between them:
strcpy( fullName, firstName);
strcat( fullName, “ “ );
strcat( fullName, lastName );
CMSC 104, Version 8/06 L25Strings.ppt 7
Built-in String Functions (cont‟d)
• Extracting words (tokens) from a string:
/* get the first token (delimited by a blank) */
printf( "%s\n", strtok( b, " " ) );
/* This is more useful after you learn to use
pointers. */
CMSC 104, Version 8/06 L25Strings.ppt 8
Built-in String Functions (cont‟d)
• What if I want to get a menu choice, that is the
numbers 1 to 4 or the char „q‟? Use getchar( ) to
get the menu choice, check for „q‟ and if it is not,
then convert it to a number.
/* convert a string (ASCII) to an integer */
printf( "%d\n", atoi( "1234" ) );
/* convert a string (ASCII) to a float */
printf( "%f\n", atof( "1234.5678" ) );
CMSC 104, Version 8/06 L25Strings.ppt 9
Built-in String Functions (cont‟d)
• How long is the data in the string (not
counting the null terminator)?
stringSize = strlen( strA );
CMSC 104, Version 8/06 L25Strings.ppt 10
String Libraries
#include files:
#include
/* needed by atoi( ) and atof( ) */
#include
/* needed by str...( ) functions */
CMSC 104, Version 8/06 L25Strings.ppt 11
Sample Program
#include
#include /* needed by atoi( ) and
atof( ) */
#include /* needed by str...( )
functions */
CMSC 104, Version 8/06 L25Strings.ppt 12
Sample Program (cont‟d)
int main( void )
{
char a[ 100 ] = "Excellence";
char b[ 100 ];
char c[ 100 ] = "Failure";
int result;
CMSC 104, Version 8/06 L25Strings.ppt 13
Sample Program (cont‟d)
/* Make sure the array a is as expected */
printf( "string a is >%s%s%s%s%sExcellenceExcellenceExcellenceExcellence Excellence
Excellence< and is 21 characters long
strtok( b, " " ) gives Excellence
CMSC 104, Version 8/06 L25Strings.ppt 20
Sample Program Output (cont‟d)
=============
string a = Excellence string c = Failure
strcmp( a, c ) gives -1
strcmp( c, a ) gives 1
strcmp( a, "Excellence" gives 0
After strcmp( "CMSC", "IFSM" ), result is -1
After strcmp( "IFSM", "CMSC" ), result is 1
After strcmp( "CMSC", "CMSC" ), result is 0
CMSC 104, Version 8/06 L25Strings.ppt 21
Sample Program Output (cont‟d)
=============
atoi( "1234" gives 1234
atof( "1234.5678" ) gives 1234.567800
CMSC 104, Version 8/06 L25Strings.ppt 22