Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engr 0012 (04-1) LecNotes 24-01.

Similar presentations


Presentation on theme: "Engr 0012 (04-1) LecNotes 24-01."— Presentation transcript:

1 Engr 0012 (04-1) LecNotes 24-01

2 strings strings are an array of type char string declaration
// include libraries #include<string.h> // defined constants #define MAXSTRING 11 main() { // begin main // variable declaration char string1[ ] = "A string"; char string2[5]; char string3[MAXSTRING]; int numchar, order; string declaration include string library used defined constant for max length + 1 memory “map” |A| |s|t|r|i|n|g|| NULL |?|?|?|?|?| |?|?|?|?|?|?|?|?|?|?|?| Engr 0012 (04-1) LecNotes 24-02

3 strings string initialization “hardwire” at declaration
// include libraries #include<string.h> // defined constants #define MAXSTRING 11 main() { // begin main // variable declaration char string1[ ] = "A string"; char string2[5]; char string3[MAXSTRING]; int numchar, order; string initialization “hardwire” at declaration |A| |s|t|r|i|n|g|| use strcpy function // string initialization strcpy(string2,"hi"); |h|i||?|?| get it from the keyboard // string initialization printf("\nEnter a string \n==> "); fflush(stdin); scanf("%[^\n]", string3); best - read until return |H|i| |t|h|e|r|e|!||?| // string initialization printf("\nEnter a string \n==> "); fflush(stdin); scanf(" %s", string3); not as good |H|i||?|?|?|?|?|?|?|?| Engr 0012 (04-1) LecNotes 24-03

4 strings string display string placeholder is %s string1 = A string
// displaying strings printf( "\n\nstring1 = %s", string1 ); printf( "\n\nstring2 = %s", string2 ); printf( "\n\nstring3 = %s", string3 ); string placeholder is %s string1 = A string string2 = hi string3 = Hi there! Engr 0012 (04-1) LecNotes 24-04

5 strings copying strings - strcpy
strcpy( string3, string1); printf( "\n\nstring1 = %s", string2 ); printf( "\n\nstring3 = %s", string3 ); copies contents of second argument into first argument ==> copies string1 into string3 string1 before |A| |s|t|r|i|n|g|| string3 before |H|i| |t|h|e|r|e|!||?| string3 after |A| |s|t|r|i|n|g|||?| string1 = A string string3 = A string display Engr 0012 (04-1) LecNotes 24-05

6 strings string length - strlen display
numchar = strlen( string3 ); printf("\n\nnum char in \"%s\" : %d", string3, numchar ); strlen counts characters until NULL is reached string3 |A| |s|t|r|i|n|g|||?| num char in "A string" : 8 display Engr 0012 (04-1) LecNotes 24-06

7 strings comparing strings - strcmp display
// compare strings order = strcmp( string1, string2); if ( order < 0 ) { printf( "\n\n%s before %s", string1, string2 ); } else if ( order == 0 ) { printf( "\n\n%s equals %s", string1, string2 ); } else { printf( "\n\n%s after %s", string1, string2 ); } strcmp determines alphabetical order of two strings string1 |A| |s|t|r|i|n|g|| string2 |h|i||?|?| A string before hi display Engr 0012 (04-1) LecNotes 24-07

8 strings parameters in function calls strings are an array of type char
parameter useage follows same rules as any array // prototypes void getstring( char string[] ); formal parameter requires type (char), name, and [ ] to designate array main() { … getstring( string3 ); actual parameter requires local name Engr 0012 (04-1) LecNotes 24-08


Download ppt "Engr 0012 (04-1) LecNotes 24-01."

Similar presentations


Ads by Google