I have had a lot of request for source code on string functions implemented in C and C++. I do not have this code but it can easily be derived if you understand some simple concepts about how strings are implemented in C and C++. People want to know how strings are used by C and C++ and which libraries to use on them and other functionality that can be applied to them so here is a brief introduction.
From here on in we will use an interpretation that is compatible with both languages.
Definition
A string in C++ and C can be implemented simply by use of an array of characters.
i.e. char myString[SIZE_OF_STRING];
Each character in the string can be retrieved or set by using the subscript of the array to activate that position within the string.
e.g. myString[2] = ‘a’;
sets the third character of myString to the letter ‘a’.
As you may have noticed the element position address and the character in string is different by a magnitude of one. This is because like all C++ and C array addressing the first element/character starts at position 0.
Addressing character positions can be a very powerful and dangerous ability when it comes to using string manipulation. It is possible that you address beyond (and in some cases below) your strings assigned memory area which can lead to all sorts of debugging.
Therefore the addressable space of a string is one minus SIZE_OF_STRING.
However to complicate matters further you need to insert a special end of string delimiter at the end of your string as part the string standard to indicate when your string has finished. This special character is indicated below,
myString[SIZE_OF_STRING-1] = ‘\0’;
The ‘\0’ character actually represents the Hex value 00 and all C and C++ library functions interpret that as the end of string marker.
Therefore the maximum addressable space your text/character are allowed to occupy is SIZE_OF_STRING-2.
Examples
The following examples assume you have knowledge of how arrays, pointers and how functions are called in C or C++ (please visit http://www.howstuffworks.com/c.htm)
/* Example of copying one string to another */
/*first string is assigned a set memory size and assigned values plus an end of character
marker (size is 27)*/
char firstString[] = “abcdefghijklmnopqrstuvwxyz”;
/* second string is just assigned memory of 30 characters */
char secondString[30];
int i;
/* Loop through all character up to the maximum size of destination array
or size of string in source array */
for(i=0;i<30-1 && firstString[i] != ‘\0’;i++)
{
/* activate same elements in each array for assigning and evaluating */
secondString[i] = firstString[i];
}
/* Set end of string marker */
secondString[i] = ‘\0’;
Notice in this simple example that I interchange array and string.
More sophisticated examples are show below first a function to calculate string size, then a function to copy one word from within a string into another.
/* function to return the size of a string.
Assumes string is delimited by end of string marker ‘\0’
*/
int myLen(char inputString[]){
/* currentPos in the string */
int currentPos;
/* loop through and increment until the end of string delimiter is found */
for(currentPos =0;inputString[currentPos] != ‘\0’;currentPos++);
/* return the current position plus one as this is the length of the string */
return currentPos+1;
}
/* This function will take the position of one word and the start position of a word after the word to be extracted and copy it into an array specified in the command line arguments. */
void copyWord(char sourceSentence[], int endBeforeWord, int startWordAfter, char outputWord[]){
int i;
/* set leading Spaces to true */
int leadingSpaces = 1;
/* set destination array position to start of array */
int copyPos = 0;
/*loop through spaces until word is reached and finished */
for(i = endBeforeWord; i < startWordAfter; i++) {
/* if not leading spaces then copy word into outputWord */
if(sourceSentence [i] != ' ' && leadingSpaces != 1) {
outputWord [++(copyPos)] = sourceLine[i];
leadingSpaces = 0;
}
/* Right trim any spaces and characters that are found once the first word has been found */
if(sourceSentnece[i] == ‘ ‘ && leadingSpaces == 0){
outputWord [++(copyPos)] = ‘\0’;
return ;
}
}
}
The last example demonstrates a primitive example of how substring function could be implemented in C and C++. Due to it primitive make up of strings C and C++ can be one of the most efficient languages to manipulate string based algorithms.
These pages are maintained by Mark Riddley - mark_riddley@yahoo.co.uk . Please mail if you have any comments, suggestions, queries, etc.