String
`
ëor example:
char name[50] = ³DAVE´;
`
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
T h i s \0 a s t r I n g . \0
Array of char:
char str[5] = {'l', 'i', 'n', 'u', 'x'};
char str[6] = {'l', 'i', 'n', 'u', 'x', '\0'};
Declarations
£ |
Ëinclude <stdio.h>
Ëinclude <string.h>
int main()
{
/* Example 1 */
char string1[ ] = "A string declared as an array.\n";
/* Example 2 */
char *string2 = "A string declared as a pointer.\n";
/* Example 3 */
char string3[30];
strcpy(string3, "A string constant copied in.\n");
printf (string1);
printf (string2);
printf (string3);
return 0;
}
£ |
char string3[30];
strcpy copies a string, including the null character terminator from the
source string to the destination. This function returns a pointer to the
destination string, or a NULL pointer on error. Its prototype is:
char *strcpy(char *dst, const char *src);
strncpy is similar to strcpy, but it allows the number of characters to be
copied to be specified. If the source is shorter than the destination,
than the destination is padded with null characters up to the length
specified. This function returns a pointer to the destination string, or a
NULL pointer on error. Its prototype is:
char *strncpy(char *dst, const char *src, size_t len);
2
This function appends a source string to the end of a
destination string. This function returns a pointer to the
destination string, or a NULL pointer on error. Its prototype is:
char *strcat(char *dst, const char *src);
This function appends at most N characters from the source
string to the end of the destination string. This function returns a
pointer to the destination string, or a NULL pointer on error. Its
prototype is:
char *strncat(char *dst, const char *src, size_t N);
2
This function returns the length of a string,
not counting the null character at the end.
That is, it returns the character count of the
string, without the terminator. Its prototype is:
char *strtok(char *s1, const char *s2) -- break the string pointed
to by s1 into a sequence of tokens, each of which is delimited
by one or more characters from the string pointed to by s2.
char *strtok_r(char *s1, const char *s2, char **lasts) -- has the
same functionality as strtok() except that a pointer to a string
placeholder lasts must be supplied by the caller.
"
#
Output:
± printf(³This is a string: %s´, str1);
Input:
± scanf(³%s%ld%lf%s´, str1, &value1, &value2, str2);
± gets
± scanline
ëile operations
± fopen, fclose, fgets«