Monday, December 26, 2011

Coding lessons: C and String functions (lessons 13)

Firstly, Merry Christmas!!

Two lessons ago I discussed arrays, and how to use them.
in the last lessons I discussed that Strings were in fact just arrays of characters.

We noted (with code) that it was easy enough to poke values into these arrays, and pretty simple to read them back out using the %s operator, in the same way that we'd used %f for reading floating point numbers, and %i for integers and %c for characters.

It's all very well looking at how a program can display strings, but what we really want is to learn how to manipulate those strings.

This lesson will introduce some functions for manipulating strings.

Firstly, it's important to say that the C language has pretty much no built in functions for using strings. so far we've only been including the standard input/output library (stdio.h), but now we're going to need to start using a library that's been written especially to use strings.

string.h

the first function that we'll look at is strlen.

strlen determines the length of a string, it's a number, clearly an integer number you don't get  half a letter in a string.

Number = strlen(string);

so a quick program that makes use of this is

#include<stdio .h>
#include<string .h>
int main()
{
char hw[] = "Hello World";
int num;
num = strlen(hw);
printf("the string %s is %i characters long", hw, num);
}


Now let's look at copying strings between different arrays.

to copy a string, we need to use strcpy (string copy)

strcpy is a function that you pass two parameters to
strcpy(destination, source);

this is different from how you're used to copying thins saying copy this source to that destination.

here's a quick program to look at strcpy

#include<stdio .h>
#include<string .h>
int main()
{
char hw[] = "Hello World";
char ds[20];
strcpy(ds, hw);
printf("the start string is %s\r\n this is copied to string 2 which says %s", hw, ds);
}

The next thing that we're going to do is look at comparing strings.
The function for comparing strings is called strcmp
There are three possible results for the strcmp function
the are 0, (they are identical)
Less than zero, (the strings are different and the first string is alphabetically before the second string, (e.g A is alphabetically before B).
if more than zero is returned this tells you that the first string is alphabetically after the second string. (e.g C is alphabetically after B).

#include<stdio .h>
#include<string .h>
int main()
{
char string1[] = "hello";
char string2[] = "world";
int result;
result = strcmp(string1, string2);
if (result < 0)
{
printf("the result is less than zero (%d), so string 1 (%s) is alphabetically before string2 (%s)", result, string1, string2);
}
if (result == 0)
{
printf("the result is zero (%d), so string 1 (%s) is athe same as string2 (%s)", result, string1, string2);
}
if (result > 0)
{
printf("the result is greater than zero (%d), so string 1 (%s) is alphabetically after string2 (%s)", result, string1, string2);
}
}


Now we'll look at adding strings together, to add strings together, Or catenate strings we use the strcat function.
when using strcat, like strcpy the name of the destination string comes as the first variable passed to the function, then data you want to poke into it comes next.

#include<stdio .h>
#include<string .h>
int main()
{
char firstname = "john";
char lastname = "doe";
char fullname[50]; /*make an array at least big enough to hold the greatest expected data entry*/
/*Print the last name string*/
printf("uncorrected last name = %s\r\r\n", lastname);

/*see we need to write null values to each box of the string array -we'll use a for loop if we don't do this then some odd things can be displayed!*/
for (i=0;i<=49;i++)
{
fullname[i]='\0';
}
/*first put the firstname into the string*/
strcat(fullname, firstname);
/*now put last name in after that*/
strcat(fullname, lastname);
printf("fullname = %s", fullname);

}

And for now that's all I'm going to write about strings.

No comments: