Sei sulla pagina 1di 22

Strings

Strings Topics • String Libraries • String Operations • Sample Program Reading • Sections 8.1 -

Topics

String Libraries

String Operations

Sample Program

Reading

Sections 8.1 - 8.7

String

String • A string is a character array that has a marker to show where the

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.)

Array Constraint

Array Constraint • There are now built-in operators to manipulate arrays, except to initialize them when

There are now built-in operators to manipulate arrays, except to initialize them when you declare the array:

char firstname[ 50 ] = “Sue”;

Missing Operators

Missing Operators • There is no string assignment operators. • There are not string comparison 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.

Built-in String Functions

Built-in String Functions • String assignment: strcpy( destination, source ) char name[ 25 ]; /* contains

String assignment:

strcpy( destination, source )

char name[ 25 ]; /* contains nothing */ strcpy( name, “Hilton” ); /* name now contains “Hilton” */

Built-in String Functions (cont’d)

Built-in String Functions (cont’d) • String comparison: strcmp( strA, strB ); o o o If strA

String comparison:

strcmp( strA, strB );

o

o

o

If strA comes after strB, the function returns a positive number.

Is strB comes last, the function returns a negative number.

If strA and strB are the same thing, the function returns

a zero.

result = strcmp( “CMSC”, “IFSM” ); result = strcmp( “IFSM”, “CMSC” );

result = strcmp( “CSMC”, “CMSC” ); /* zero */

/* negative */ /* positive */

Built-in String Functions (cont’d)

Built-in String Functions (cont’d) • String combination: strcat( destination, source ) o o The source is

String combination:

strcat( destination, source )

o

o

The source is not changed.

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 );

Built-in String Functions (cont’d)

Built-in String Functions (cont’d) • Extracting words (tokens) from a string: /* get the first token

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. */

Built-in String Functions (cont’d)

Built-in String Functions (cont’d) • What if I want to get a menu choice, that is

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" ) );

Built-in String Functions (cont’d)

Built-in String Functions (cont’d) • How long is the data in the string (not counting the

How long is the data in the string (not counting the null terminator)? stringSize = strlen( strA );

String Libraries

String Libraries # include files: #include <stdlib.h> /* needed by atoi( ) and atof( ) */

#include files:

#include <stdlib.h> /* needed by atoi( ) and atof( ) */ #include <string.h>

/* needed by str

(

) functions */

Sample Program

Sample Program #include <stdio.h> #include <stdlib.h> /* needed by atoi( ) and atof( ) */ #include

#include <stdio.h> #include <stdlib.h> /* needed by atoi( ) and atof( ) */ #include <string.h> /* needed by str functions */

( )

Sample Program (cont’d)

Sample Program (cont’d) int main( void ) { char a[ 100 ] = "Excellence"; char b[

int main( void )

{

char a[ 100 ] = "Excellence"; char b[ 100 ]; char c[ 100 ] = "Failure"; int result;

Sample Program (cont’d)

Sample Program (cont’d) /* Make sure the array a is as expected */ printf( "string a

/* Make sure the array a is as expected */ printf( "string a is >%s<\n", a ); /* assign a to b */ strcpy( b, a ); printf( "After strcpy(b, a), string b is now >%s<\n", b );

Sample Program (cont’d)

Sample Program (cont’d) printf( "\n=============\nString b = >%s< and is %d characters long\n", b,

printf( "\n=============\nString b = >%s< and is %d characters long\n", b, strlen( b ) ); /* put in a space and add the array a to what is in array a */ strcat ( b, " " ); printf( "After strcat(b, \" \"), string b = >%s< and is %d characters long\n", b, strlen( b ) ); strcat ( b, a ); printf( "After strcat(b, a), string b = >%s< and is %d characters long\n", b, strlen( b ) );

Sample Program (cont’d)

Sample Program (cont’d) /* get the first token (delimited by a blank) */ printf( "strtok( b,

/* get the first token (delimited by a blank) */ printf( "strtok( b, \" \" ) gives %s\n", strtok( b, " " ) );

printf( "\n=============\n");

printf( "string a = %s

string c = %s\n", a, c );

/* "Excellence" comes before "Failure", so print a negative number */ printf( "strcmp( a, c ) gives %d\n", strcmp( a, c ) );

Sample Program (cont’d)

Sample Program (cont’d) /* " Failure " comes before “Excellence ", so print a positive number

/* " Failure " comes before “Excellence ", so print a positive number */ printf( "strcmp( c, a ) gives %d\n", strcmp( c, a ) );

/* "Excellence" is the same as "Excellence", so print zero */ printf( "strcmp( a, \"Excellence\" gives %d\n", strcmp( a, "Excellence" ) );

Sample Program (cont’d)

Sample Program (cont’d) /* negative */ printf( "After strcmp( \"CMSC\", \"IFSM\" ), result is

/* negative

*/ printf( "After strcmp( \"CMSC\", \"IFSM\" ), result is %d\n", result);

result = strcmp( "CMSC", "IFSM" );

/* positive */

printf( "After strcmp( \"IFSM\", \"CMSC\" ), result is %d\n", result);

result = strcmp( "CMSC", "CMSC" ); /* zero */ printf( "After strcmp( \"CMSC\", \"CMSC\" ), result is %d\n", result);

result = strcmp( "IFSM", "CMSC" );

Sample Program (cont’d)

Sample Program (cont’d) printf( "\n=============\n"); /* convert a string to an integer */ printf( "atoi(

printf( "\n=============\n"); /* convert a string to an integer */ printf( "atoi( \"1234\" gives %d\n", atoi( "1234" ) );

/* convert a string to a float */ printf( "atof( \"1234.5678\" ) gives %f\n", atof( "1234.5678" ) );

return 0;

}

Sample Program Output

Sample Program Output string a is >Excellence< After strcpy(b, a), string b is now >Excellence<

string a is >Excellence< After strcpy(b, a), string b is now >Excellence<

============= String b = >Excellence< and is 10 characters long After strcat(b, " "), string b = >Excellence < and is 11 characters long After strcat(b, a), string b = >Excellence Excellence< and is 21 characters long strtok( b, " " ) gives Excellence

Sample Program Output (cont’d)

Sample Program Output (cont’d) ============= string a = Excellence strcmp( a, c ) gives -1 strcmp(

============= string a = Excellence

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

string c = Failure

Sample Program Output (cont’d)

Sample Program Output (cont’d) ============= atoi( "1234" gives 1234 atof( "1234.5678" ) gives

============= atoi( "1234" gives 1234 atof( "1234.5678" ) gives 1234.567800