Strings

A string is a sequence of characters. There is an order. In English it is left to right. They are really defined by a set of operations:

  1. read,write
    get characters from the user and put in a string Take a string and print the characters
  2. length
    count the number of chars in the string
  3. insert
    put a string inside of another string
  4. delete
    remove some chars from the string
  5. findchar
    return the location of a single char in the string
  6. substr
    find the location of a string in another string
  7. concat
    append one string to another

In C++, strings, or cstrings are represented as an array of characters. To mark the end of the string, we store the NUL character in the array element after the last character. Remember, the array holds the string, it is not the string. The string can be shorter than the array.

For example, the string "hello" is stored in a character array like this:
char str[6];
h e l l o \0 (NUL)

If we were to make a pointer to this array, it would be a character pointer because the type of the things in the array is character. In general, if you have an array of some type, a pointer to this array is of the type a pointer to the type. For example. if we have an array on integers like this,
int example_array[10];
To make a pointer to this array we do
int *ptr;
Think of it as if we make a pointer to the first thing in the array.

Here are some bits of code to manipulate strings


		// find the length of a string
		int
		strlen(char * ptr)
		{
			int n=0;
			while(ptr[n] != '\0') n++;
			return n;
		} // strlen
or

		// find the length of a string
		int
		strlen(char ptr[])
		{
			int n=0;
			while(ptr[n]) n++;
			return n;
		} // strlen