C Tutorial

Back to Tutorials

Next Lesson


Lesson 9: Strings Revisited

Lesson Requirements
A C compiler.

Instructions on using the LCCWin32 Compiler.

Lesson Summary
A common complaint about QuakeC was the lack of any decent string handling routines. With Quake2 using C :) lets now explore what we can do. For those of you that wanted a bot that could understand what the player said, you could now write a parser and actually make the bot do so!


 In most cases we simple want to concatenate strings together. We can do this several ways, depending upon what the strings are. Firstly lets consider our basic string:

char mystring[10];

char my_other_string[100];

now suppose we want to add mystring onto the end of my_other_string we would normally use the function:

char *strcat( char *strDestination, const char *strSource );

which concatenates the string strSource onto the end of the string strDestination so for our example we have:

strcat(my_other_string, mystring);

 In other cases we may wish to place the contents of a variable using a string, in which case we would normally use the function:

int sprintf( char *buffer, const char *format [, argument] ... );

which is effectively the same as our printf except the first arguement is a string we wish to print to. We could you this to build up a line of information about a character, whoose attributes are stored in the variables health and ammo i.e.

	int ammo;
	int health;
	char name[10];
	char message[100];
	sprintf(message, "%s, your health is %d, and you only have %d ammo left...\n", name, health, ammo);

Would set the string message to: Bob, your health is 10, and your only have 20 ammo left... (assuming the variables had those values).




Back to Tutorials

Next Lesson


Copyright © 1997, John Crickett & Neil Henderson.
Legal Information