Anda di halaman 1dari 29

|  


  

String
`     

Strings in C are represented by arrays of characters.


The end of the string is marked with a special
character, the Y , which is simply the
character with the value 0 (or \0 ).

ëor example:
char name[50] = ³DAVE´;
`       

char a[50] = ³This is \n a string.´;


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
T h i s \n a s t r I n g . \0

printf("This is a string value. Beep! Beep! \7\7");


   

`hat output of printf(a); if a is:

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

`hat is the length of string a?

`hat if char a[50] does not contain µ\0¶ at all?

Are following correct, given char a[6];


a[6]=µ7¶;
a[5]=³7´;
a[5]=7;
a++;
£    |    

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 string1[] = "A string declared as an array.\n";


This is usually the best way to declare and initialize
a string. The character array is declared explicitly.
There is no size declaration for the array; just
enough memory is allocated for the string, because
the compiler knows how long the string constant is.
The compiler stores the string constant in the
character array and adds a null character (\0) to the
end.
£    |    

char *string2 = "A string declared as a pointer.\n";


The second of these initializations is a pointer to an array
of characters. Just as in the last example, the compiler
calculates the size of the array from the string constant
and adds a null character. The compiler then assigns a
pointer to the first character of the character array to the
variable string2.
l Most string functions will accept strings declared in
either of these two ways.
£    |    

char string3[30];

Declaring a string in this way is useful when you


don't know what the string variable will contain, but
have a general idea of the length of its contents (in
this case, the string can be a maximum of 30
characters long). The drawback is that you will
either have to use some kind of string function to
assign the variable a value, as the next line of code
does ( strcpy(string3, "A string constant copied
in.\n");), or you will have to assign the elements of
the array the hard way, character by character.
2  

A string array is an array of strings, which, of


course, are themselves arrays of characters;
in effect, a string array is a two-dimensional
character array.
char *menu[] =
{
" -------------------------------------- ", int main()
"| ++ MENU ++ |", {
"| ~~~~~~~~~~~~ |", int line;
" | (0) Edit Preferences |", for (line = 0; line < 13; line++)
" | (1) Print Charge Sheet |", {
" | (2) Print Log Sheet |", printf ("%s\n", menu[line]);
" | (3) Calculate Bill |", }
" | (q) Quit |", return 0;
"| |", }
"| |",
" | Please enter ur choice. |",
"| |",
" -------------------------------------- ³
};
î

Because C has no built-in facilities for manipulating


entire arrays (copying them, comparing them, etc.), it
also has very few built-in facilities for manipulating
strings:
char firstname[50],lastname[50],fullname[100];

firstname= "Arnold"; /* Illegal */


lastname= "Schwarznegger"; /* Illegal */
fullname= "Mr"+firstname
+lastname; /* Illegal */
2    

The GNU C Library provides a number of


very useful functions which handle strings.
Here is a list of the more common ones. To
use the functions beginning with ato, you
must include the header file stdlib.h; to use
the functions beginning with str, you must
include the header file string.h.
2    

int atoi(const char *nptr);

double atof(const char *nptr);

long atol(const char *nptr);


2    
/*atof Converts an ASCII string to its floating-point equivalent; for
example, converts -23.5 to the value -23.5. */
Ëinclude <stdio.h>
Ëinclude <stdlib.h>
int main()
{
double my_value;
char my_string[] = "+1776.23";
my_value = atof(my_string);
printf("%f\n", my_value);
return 0;
}
The output from the above code is 1776.230000.
2    

/* atoi Converts an ASCII string to its integer equivalent; for


example, converts -23.5 to the value -23. */
int my_value;
char my_string[] = "-23.5";
my_value = atoi(my_string);
printf("%d\n", my_value);
/*atol Converts an ASCII string to its long integer equivalent; for
example, converts +2000000000 to the value 2000000000. */
long my_value;
char my_string[] = "+2000000000";
my_value = atol(my_string);
printf("%ld\n", my_value);
2    



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 compares two strings. If the first string is greater


than the second, it returns a number greater than zero. If the
second string is greater, it returns a number less than zero. If
the strings are equal, it returns 0. Its prototype is:
int strcmp(const char *first, const char *second);

 

This function compares the first N characters of each string. If


the first string is greater than the second, it returns a number
greater than zero. If the second string is greater, it returns a
number less than zero. If the strings are equal, it returns 0. Its
prototype is:
int strncmp(const char *first, const char *second, size_t N);
2    

The strcmp() function 


 compares the
two input strings and returns:
±  
-- if string1 is lexically less than string2
± 
-- if string1 and string2 are lexically equal
±   
-- if string1 is lexically greater than string2
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:

size_t strlen(const char *str);


2    

int strcasecmp(const char *s1, const char *s2)


-- case insensitive version of strcmp().

int strncasecmp(const char *s1, const char


*s2, int n) -- case insensitive version of
strncmp().
2    

To use any of these functions in your code,


the header file "strings.h" must be included. It
is the programmer's responsibility to check
that the destination array is large enough to
hold either what is copied or appended to it.
C performs no error checking in this respect.
See the array tutorial for more details. The
data type size_t is equivalent to unsigned
integer.
2    

The use of most of the functions is


straightforward, for example:

char *str1 = "HELLO";


char *str2;
int length;
length = strlen("HELLO"); /* length = 5 */
(void) strcpy(str2,str1);
2    

The strncat(), strncmp,() and strncpy() copy functions


are string restricted version of their more general
counterparts. They perform a similar task but only up
to the first n characters. Note the the NULL
terminated requirement may get violated when using
these functions, for example:
char *str1 = "HELLO";
char *str2;
int length = 2;
(void) strcpy(str2,str1, length); /* str2 = "HE" */
l îl î|lî£ `
2    !

char *strchr(const char *string, int c) -- ëind first


occurrence of character c in string.

char *strrchr(const char *string, int c) -- ëind last


occurrence of character c in string.

char *strstr(const char *s1, const char *s2) -- locates


the first occurrence of the string s2 in string s1.

char *strpbrk(const char *s1, const char *s2) --


returns a pointer to the first occurrence in string s1 of
any character from string s2, or a null pointer if no
character from s2 exists in s1
2    !

size_t strspn(const char *s1, const char *s2) -- returns the


number of characters at the begining of s1 that match s2.

size_t strcspn(const char *s1, const char *s2) -- returns the


number of characters at the begining of s1 that m Y  match s2.

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.
 
  "  # 

void *memchr (void *s, int c, size_t n) -- Search for a


character in a buffer .

int memcmp (void *s1, void *s2, size_t n) -- Compare


two buffers.

void *memcpy (void *dest, void *src, size_t n) --


Copy one buffer into another .

void *memmove (void *dest, void *src, size_t n) --


Move a number of bytes from one buffer lo another.

void *memset (void *s, int c, size_t n) -- Set all bytes


of a buffer to a given character.
2 |
$ 


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«

Anda mungkin juga menyukai