1 / 12

Strings

Strings. Includes: #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* needed by atoi( ) and atof( ) */ #include &lt;string.h&gt; /* needed by str...( ) functions */. Copy One Array to Another. /* assign a to b */ strcpy( b, a ); printf( &quot;%s<br>&quot;, b );. Add One Array to Another.

ledell
Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Copy One Array to Another • /* assign a to b */ • strcpy( b, a ); • printf( "%s\n", b );

  3. Add One Array to Another • strcpy( b, a ); • strcat ( b, " " ); • strcat ( b, a );

  4. Compare Strings • /* "Gary Burt" comes before "Mike Burt", so print a negative number */ • printf( "%d\n", strcmp( a, c ) ); • /* "Mike Burt" comes before "Gary Burt", so print a positive number */ • printf( "%d\n", strcmp( c, a ) ); • /* "Gary Burt" is the same as "Gary Burt", so print zero */ • printf( "%d\n", strcmp( a, "Gary Burt" ) );

  5. Getting Words From a String • /* get the first token (delimited by a blank) */ • printf( "%s\n", strtok( b, " " ) );

  6. Convert String to Numeric • /* convert a string to an integer */ • printf( "%d\n", atoi( "1234" ) ); • /* convert a string to a float */ • printf( "%f\n", atof( "1234.5678" ) );

  7. Find Length Of A String • printf( "b is %d characters long\n", strlen( b ) );

  8. Sample Program • #include <stdio.h> • #include <stdlib.h> /* needed by atoi( ) and atof( ) */ • #include <string.h> /* needed by str...( ) functions */ • int main( void ) • { • char a[ 100 ] = "Gary Burt"; • char b[ 100 ]; • char c[ 100 ] = "Mike Burt"; • /* Make sure the array a is as expected */ • printf( "%s\n", a );

  9. Sample Program • /* assign a to b */ • strcpy( b, a ); • printf( "%s\n", b ); • /* put in a space and add the array a to what is in array a */ • printf( "b is %d characters long\n", strlen( b ) ); • printf( "%s\n", b ); • strcat ( b, " " ); • printf( "b is %d characters long\n", strlen( b ) ); • printf( "%s\n", b ); • strcat ( b, a ); • printf( "b is %d characters long\n", strlen( b ) ); • printf( "%s\n", b );

  10. Sample Program • /* "Gary Burt" comes before "Mike Burt", so print a negative number */ • printf( "%d\n", strcmp( a, c ) ); • /* "Mike Burt" comes before "Gary Burt", so print a positive number */ • printf( "%d\n", strcmp( c, a ) ); • /* "Gary Burt" is the same as "Gary Burt", so print zero */ • printf( "%d\n", strcmp( a, "Gary Burt" ) ); • /* get the first token (delimited by a blank) */ • printf( "%s\n", strtok( b, " " ) );

  11. Sample Program • /* convert a string to an integer */ • printf( "%d\n", atoi( "1234" ) ); • /* convert a string to a float */ • printf( "%f\n", atof( "1234.5678" ) ); • return 0; • }

  12. Output • Gary Burt • Gary Burt • b is 9 characters long • Gary Burt • b is 10 characters long • Gary Burt • b is 19 characters long • Gary Burt Gary Burt • -1 • 1 • 0 • Gary • 1234 • 1234.567800

More Related