1 2 3 4 5 6 7 8 9 10

Operations on strings

char* s = "My sample string";
we can also use the typedef statement to form a user type

typedef char* String;// #define String char* String s = "My sample string";

We can now find the length of this string with:

int len = strlen (s);

// will yield 16

but if we want to copy this string into an array then we would need a declaration like:

char letters [1+strlen(s)];

and then use the copy procedure strcpy()

strcpy (&letters[0], s);

This however is not common usage Note the prototype for the strcpy function

// Why 1+?

char* strcpy ( char* StrNew, const char* StrOrig);

Questions:
why is this a char* function and not a void function?

why is the second parameter of type const char* ?

4 February, 1998

5

Copyright University of Alberta