Here introduces C-style 'string' (mostly char[]
) operation.
It's important that with classes and many more useful and Memory Safe functions, string
shall not be processed with these functions.
-
strcpy(dst,source),return dst
copy source to destination, returning the address of destination. be cautious that it can cause memory leak. example:char b[5]={0};
char a[]="abcdefghijklmn";
cout << (int)strcpy(b,a);
you'll get an address.cout << ' ';
for(int i = 0;i<=15;i++)cout<<b[i];
returns:
1
26422204 abcdefghijklmn#
#:memory leak^ -
strncpy(dst,src,n),return dst
copy n characters at most from source to destination. returning the address of destination. (better than strcpy to avoid memory leak.) -
strcat(dst,src),return dst
add src to the end of dst. Returning the address of dst. -
strncat(dst,src,n),return dst
analogise with 2. -
strlen(s),return int
length of a string(to the \0, not the size of a list). -
strcmp(s1,s2),return int
return (s1-s2). -
strncmp(s1,s2,n),return int
,anal. with 2. -
strchr(s,ch),return addr
return the address of the first ch in s.example:
char a[]="abcdefghijklmn";
cout<<strchr(a,'f');
returns:6422278
example:
char a[]="abcdefghijklmn";
cout<<(int)strchr(a,'q');
returns:0
-
strrchr(s,ch),return addr
return the address of the last ch in s. -
strstr(s1,s2),return addr
anal with 7,but finding s2.