Basic Strings

Overview

Declaring and Allocating Strings

Very soon, you will need to separate in your mind declaration from allocation. In this section, I am only showing the syntax for declaring and allocating the string at the same time.
Declaring a variable means that you specify the name and the type.
Allocating a variable is when the space where that variable will reside is created.
For the variables we've seen so far, those two things happen at the same time. This pairing of the two is about to end.

Initializing Strings

Initializing a variable is placing the first values into that variable. It is important to initialize strings (at least to "") so that there is a '\0' present in the allocated space.

Strings as Arrays

Strings are much like arrays, except for two constraints: This changes what the loop looks like to step through the string. Let's find the length of the string (the number of characters before '\0'):
unsigned int stringlength(char s[])
{
	int length = 0;
	for (length = 0; s[length] != '\0'; length++)
		;		// all of the work is being done in the for loop syntax!
	return length;
}

Strings as Strings

While you may not operate on an entire array at once. However, you can use the string library, provided by C. Make sure you #include <string.h> Here are a few common ones.
Function prototype Functionality
strlen(char s[]) Calculates number of characters before '\0'
strncpy( char dest[], char src[], unsigned int n) Copies characters from source to destination. Will copy through '\0' or n characters, which is reached first.
strcat( char dest[], char src[], unsigned int n) Copies characters from source to the *end* of destination (concatenation). Will copy through '\0' or n characters, which is reached first.
strcmp(char str1[], char str2[]) Compares str1 and str2. If str1 < str2 (previous in abc order), return -1. Same, return 0. >, return 1
strlwr(char s[]), strupr(char s[]) converts string to lowercase or uppercase, respectively
Below shows a relatively meaningless function that utilizes several of the string library functions.
#include 
#include 
#include 

void do_some_stuff(char s1[], char s2[])
{
	int s1_len = strlen(s1);
	int s2_len = strlen(s2);

	strncpy(s1, s2, s1_len);

	strlwr(s1);
	strlwr(s2);
	if (strcmp(s1, s2) == 0)
		printf("%s == %s\n",s1, s2);
	else
		printf("%s != %s\n",s1, s2);

}