Title: Lessons from CST exercise
1Engr 0012 (04-1) LecNotes 24-01
2strings
strings are an array of type char
memory map
Engr 0012 (04-1) LecNotes 24-02
3strings
// include libraries include //
defined constants define MAXSTRING
11 main() // begin main // variable
declaration char string1 "A string"
char string25 char string3MAXSTRING
int numchar, order
string initialization
A string?
hi???
get it from the keyboard
Hi there!??
Hi?????????
Engr 0012 (04-1) LecNotes 24-03
4strings
string display
// 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
5strings
copying strings - strcpy
// copying strings 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
Engr 0012 (04-1) LecNotes 24-05
6strings
string length - strlen
// string length numchar strlen( string3
) printf("\n\nnum char in \"s\" d",
string3, numchar )
strlen counts characters until NULL is reached
Engr 0012 (04-1) LecNotes 24-06
7strings
comparing strings - strcmp
// compare strings order strcmp( string1,
string2) if ( order "\n\ns before s", string1, string2 )
else if ( order 0 ) printf( "\n\ns
equals s", string1, string2 ) else
printf( "\n\ns after s", string1, string2 )
strcmp determines alphabetical order of two
strings
Engr 0012 (04-1) LecNotes 24-07
8strings
parameters in function calls
strings are an array of type char
parameter useage follows same rules as any array
Engr 0012 (04-1) LecNotes 24-08