Lecture 17: strings |
string
We will now see the implications of this simple definition. |
Naively we might now think that assigning a value to this variable
could
be done with
n = "Benfica";
but remember that n is a pointer
to char (see lecture 15) and the right side of
the
= isn't (it is a string constant). The way to do it would be to assign
a value to each element of the array:
n[0] = 'B';
n[1] = 'e';
n[2] = 'n';
n[3] = 'f';
n[4] = 'i';
n[5] = 'c';
n[6] = 'a';
n[7] = '\0';
With on the left side of each = an element of the array (char) and
on the right side a character constant. Therefore the assignements are
correct. (Note the '\0' character at the end, which signifies
end-of-string.)
This, however is very clumsy. For this reason and in general to
facilitate
working with strings, there exist many instructions to help working
with
strings. These are defined in the library string.h and when
we
want to use them we have to "include" the library in the beginning of
the
program with
#include <string.h>
strcpy | copy contents of one string to another |
strcat | adds one string to another |
strcmp | compares two strings |
strlen | returns the length (number of characters, excluding \0) of the string |
strstr | look for position of one string in another string |
strcpy |
short for "string copy". copies contents of one string to another. |
function definition: int *strcpy(char *s1, const char
*s2)
copies contents of s2 to s1. |
This is a function that takes two parameters, two strings
(array of
char, therefore pointer to char) s1 and s2 and
copies
contents of s2 to s1. The changes to s1
are
permanent. The function returns an int indicating the success
of the operation. For the moment we can ignore this result, to not
further
complicate matters. Also we will ignore the word const in
front
of the second parameter declaration. An example:
char n[10]; strcpy(n, "Benfica"); printf("%s\n", n); output Benfica Effectively, what the function strcpy
is doing is copying the characters of string s2 to s1 until the
end-of-string
character is encountered:
|
strcat |
short for "string concatenate". adds string to other string |
function definition: char *strcat(char
*s1, const
char *s2)
adds s2 at end of s1. |
strcmp |
short for "string compare". compares two strings |
function definition: int
strcmp(const char
*s1, const char *s2)
compares s1 with s2. If equal returns 0, if alphabetically s1<s2 returns a negative number, or returns a positive number if s1>s2. |
strcpy(n, "Benfica");
strcpy(m, "Sporting");
cmp = strcmp(n, m);
printf("%s\n", n);
if (cmp==0)
printf("strings are
equal");
else
if (cmp>0)
printf("%s
is after %s", n, m);
else
printf("%s
is before %s", n, m);
output
Benfica is before Sporting
The function strcmp could be
implemented
by something like the following
// possible implementation of strcmp
while ((*s1==*s2) &&
(*s1!='\0'))
// still equal and not end-of-string?
{
s1++; // move
pointer
s1 one place
s2++; // move
pointer
s2 one place
}
return((int) *s1 - *s2); // return
difference between contents of s1 and s2
strlen |
short for "string length". Returns the number of characters in the string (not counting \0) |
function definition: int
strlen(const char
*s1)
Returns the number of characters in the string (not counting \0) |
The function strlen could be
implemented
by something like the following
cnt=0;
while (*s1 != '\0')
{
cnt++;
s1++;
}
return(cnt);
strstr |
short for "string string". Returns the position of a string in another string. |
function definition: char
*strstr(const
char *s1, const char *s2)
Returns a pointer (char array) to the position of text s2 in text s1. |
Example:
char n[30]; char *p; strcpy(n, "Benfica"); p = strstr(n, "fic");
|
for (i=0; i<3; i++)
printf("%s\n", n[i]);
output:
Benfica
Sporting
Porto