A Mid String Function for C and C+

Replicating one of the most useful string handling functions in the Visual Basic language, using C and C++.

There are three ways to obtain a nice Mid-like function, depending on the language and portability choices the programmer has made. C, C++ and C++ STD.

Introduction

String handling is one of those C programming hot topics that everyone starts out having a problem with, and ends up an expert. The journey is often perilous and fraught with debugging nightmares.

This is worse for Visual Basic programmers, used to the niceties of a language that provides some excellent string support, than anyone else. However, dig below the surface, and it is possible to find an easy to understand way to emulate the VB string handling functions like Mid, Left and Right.

This article concentrates on the Mid function. Before diving in, however, the reader might like to refresh their memories:

The basic Mid function allows the programmer to write code such as:

myString = Mid(sourceString, 2, 5)

If we supplied "Hello Mid String" in sourceString, we would expect "llo M" to be returned in myString. With this in mind, let us look at some worked solutions.

A C/C++ Mid function

Thankfully, we do not have to go too far to find a simple function that will solve our problem. It is, however, simple enough that we need to provide error handling ourselves. This includes array over- and under-run, null pointer trapping, and all the baggage that comes with using the string.h library. Nonetheless, we can define our strings as follows:

char * outString;
char * inString;
int nStart, nLength;

The next task is to initialize inString, and allocate enough memory in outString to contain the result of the Mid operation. Allocating the same size string as inString ought to be enough. Once this has been done, we can use the string.h strncpy function to do the actual work:

strncpy(outString, inString+nStart, nLength);

The initialization and memory allocation is left as an exercise for the reader, but this link ought to be a good starting point. Solutions will tend to treat memory, character arrays and strings as one and the same collection of bytes, an approach which works rather well.

Using the C++ Standard String Library

Finally, the preferred way to implement a Mid function is simply to call the Standard Library substring method of the string class. It really is that easy, but , as usual, there is some overhead:

#include <iostream>
#include <stdlib.h>
#include <string>

The above just identifies the various libraries we shall be using. We might need iosteam (cin and cout) to process character input and output, stdlib.h for general memory related chores, and the string class for string handling. NExt, we need to tell the compiler/linker that we wish to use the Standard library implementations:

using namespace std;

This absolves us of the necessity to prefix our classes with <class>:: each time we need to use them. Thus, we can declare an object of class string:

string testString;

Now, we should also put a value into testString, and using iostream, we can just write a line such as:

cin testString;

Finally, the Mid-alike function (method) can be called:

cout testString.substr(0, 1);

That's all that is needed. In the end, this is the best solution for C++ programmers; C programmers will have to be content with writing a lot of error checking (bounds checking) code themselves.

Summary

As one might expect, the advice has to be to use the standard C++ String solution, where available. The reason for this is that it negates the requirement to check the pointer arithmetic, which can happily crash programs not explicitly taking care of null and bad pointers.

The reader might also like to contemplate a solution which, while less elegant than the above, uses array handling code to achieve the same result. This might be along the lines:

 for (int i = nStart; i nStart+nLength; i++) {
  if (i = strlen(szIn)) break;
  szOut[nOutPos++] = szIn[i];
 }

The above is generally easier to understand for beginner programmers; we all have to start somewhere.

Guy Lecky-Thompson, Self Portrait

Guy Lecky-Thompson - Guy W. Lecky-Thompson is the author of several technical and non-technical books, and writer at large. He has written for Dr. Dobbs ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 4+8?
Advertisement
Advertisement