Lecture 17: strings



 
 

string

 A string is an array of char

We will now see the implications of this simple definition.



As an example: declaring a string of 100 characters:
  char n[100];

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>


string.h

The following functions of string.h are interesting for us
 
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:
do
{
  *s1 = *s2;          // copy a character; copy contents of
                      //   address s2 to address s1
  s1++;               // make pointer s1 point to next character
  s2++;               // make pointer s2 point to next character
}
while (*s2 != '\0');  // repeat until end-of-string 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.
Two parameters, s1 and s2 are pointers to char (arrays of char). Again, like above, we will ignore the value (char pointer) that the function returns.
Example:
  char n[30];
  strcpy(n, "Benfica");
  strcat(n, " o glorioso");
  printf("%s\n", n);
output
  Benfica o glorioso
Effectively, the strcat function is equivalent to the following code:
while (*s1 != '\0')  // look for
  s1++;              //   end-of-string
strcpy(s1, s2);      // now copy the string s2 at place of the new
                     //   address s1 that points to \0
 
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
Example:
  char n[30], m[30];
  int cmp;

  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) 
Example:
  char n[30];
  strcpy(n, "Benfica");
  printf("%s has %d characters", n, strlen(n));
output
  Benfica has 7 characters

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");
  printf("%s", p);
output
  fica


Arrays of strings

Arrays of strings are therefore arrays of arrays of char:
For instance, if we want to store the names of three footbal teams of Portugal, we might do this with
  char n[3][10];
The picture below shows how the names can be stored in this array of strings.

for (i=0; i<3; i++)
  printf("%s\n", n[i]);
output:
 Benfica
 Sporting
 Porto


Peter Stallinga. Universidade do Algarve, 29 November 2002