Exercises, examples and other material relating to training module C206. This topic is presented on public courses Learning to Program in C
, Learning to program in C++
, Programming in C
, C and C++ Programming
As well as doing calculations on numbers, you'll want
your program to be able to handle strings of text - things
like names and addresses and product codes ... and HTML
tags and SQL queries too, perhaps. C defines strings as
char types, and there's a whole raft of standard functions
that are available to work on strings.
|Articles and tips on this subject||updated|
|3718||Splitting a record into individual data values in C|
Many data files consist of a number of records, each of which is divided into a number of fields. How do you handle such data records in a C program? You could use very low level string handling functions, but it's probably far better to step up one level and use something like the string tokeniser. ...
|3593||Chars, char arrays and strings in C. Some early cautions and pitfalls.|
A char in C is a single byte variable, and a string in an array of chars (i.e. a series of chars held in successive memory addresses) and terminated by a null (\0).
Because of this need for a terminator, you need to allocate one ADDITIONAL byte of memory / character position than the maximum number ...
|3144||Setting up arrays in C - fixed size at compile time, or dynamic|
You can store a whole series of data values of the same type in an array in C.
The easiest way to declare an array is something like:
and that will give you an array of 20 elements - which you can refer to as elements number 0 to 19, so:
history = 1234;
|3146||Strings in C|
Strings in C are null terminated char arrays ... so you always need to allocate one more array member for them than the maximum possible length - to hold a string of up to 20 chars, you need an array of 21.
Double quoted string constants are null terminated strings, and if you assign them to a variable, ...
|3122||When is a program complete?|
"Code is never completed ... it can always be improved." ... it's one of the most difficult aspects in many projects to say "yes, that does what we want and we should go for a release now rather than continuing to develop until ... until it's so late that we've missed the boat / added too much complexity ...
|2844||Learning about Regular Expressions in C through examples|
Although we more usually teach Regular Expressions on courses on Perl, Python, PHP, Ruby, etc ... there is also a standard C library, which uses the POSIX flavour of regular expressions, and I've put a short example together to "show you how".
Firstly - what is a regular expression?
It's a "pattern ...
|2843||String functions in C|
A String is a NUL terminated array of chars. Remember to allow space in the string for the NUL, and for any new line character you may have too, if you dimension it manually. And remember to add a NUL back on to the end if you extend a string by adding individual characters to it.
There are lots of ...
|1338||Handling Binary data in Tcl (with a note on C)|
In Tcl, all variables are held as strings, and most of the commands will split / divide strings at new line or space characters by default. However, there are a few commands that do NOT make that distinction and since a Tcl string (Unlike a C string) may contain any bit pattern at all, they provide a ...
Examples from our training material
|ccis.c|| Basic string and command line handling|
|chout.c|| Strings v chars|
|cli.c|| Command Line Handling|
|cli2.c|| Command line - use of pointers example|
|cli3.c|| Command line params - more complex character string arrangement|
|cli4.c|| Picking out command line options into an array |
|cli5.c|| More advanced coding style to handle argv and argc|
|cs1.c|| Comparing strings in C|
|csame.c|| Comparisons of strings in C|
|cstr.c|| String manipulation in C|
|cx.c|| String copying and manipulation|
|dynamic.c|| memory allocation and release with calloc and free|
|dynbad.c|| How NOT to do it - overrunning an array|
|fff|| Some sample data |
|lynebreak.c|| Tokenising a C string|
|makefile|| makefile for module C206|
|pothat.c|| Strings via pointers and arrays of chars|
|rabbit.c|| Structures and file i/o - C|
|range.c|| Summing numbers from the command line|
|readwrite.c|| File handling basics in C|
|reg.c|| Regular Expression matching in C|
|reg2.c|| Match and Capture - Regular Expressions|
|s4.c|| Read lines from file, tokenise them, save data into an array of structs.|
|sti1.c|| Characters and strings in C|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Topics covered in this module
Defining string variables
String manipulation functions
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.