The Art of String Manipulation: Spaces to Newlines

The C language lacks a sack of tools for manipulating strings. For most string-mangling operations, you’re pretty much left to carve out your own tools. Doing so may involve pointers, but it doesn’t necessarily have to.

Don’t fret over pointers just yet! Poking and prodding a string is entirely possible and gleefully acceptable by using array notation. In fact, I find in my own programs that I spend less time coding when I use arrays. ‘taint nuthin’ wrong with that.

As an example consider the change from this text:

These are not the droids you're looking for.

To this:

These
are
not
the
droids
you're
looking
for.

It’s a simple modification, right? The spaces in the string are converted to newlines. The change can happen in a number of ways. That’s an advantage of the C language: As programmer, you’re free to bake your own cake using your own recipe and ingredients. As long as the end result is a cake, you’re good.

In the following code, I chose to modify the string as it was output as opposed to changing the string as it sits in memory and then sending it to output. Doing the manipulation one way or another depends on the code, of course, and whether further string mangling is required. (If so, then you change the string in memory.)

This solution uses array notation and not pointers. Feel free to breathe a great sigh of relief, but don’t lean back in your puffy arm chair just yet. Here’s the code:

#include <stdio.h>

int main()
{
    char ob1[] = "These are not the droids you're looking for.\n";
    int x;

    x = 0;
    while(ob1[x])
    {
        if(ob1[x] == ' ')
            putchar('\n');
        else
            putchar(ob1[x]);
        x++;
    }

    return(0);
}

The while loop at Line 9 plows through the char array one character a time. When the value of ob1[x] is the null character, \0, marking the end of the string, the loop terminates.

Inside the loop, Line 11 checks to see whether a space character is found. If so, the newline character, \n, is displayed at Line 12. Otherwise, the character in the string, ob1[x] is displayed. Line 15 increments the index variable x to reference the next character in the array.

Overall, this should be pretty basic stuff — especially if you’ve read one of my C language books!

The first variation on this code is to replace array notation with pointers. A straight-across replacement of array notation with pointers looks like this:

#include <stdio.h>

int main()
{
    char ob1[] = "These are not the droids you're looking for.\n";
    char *x;

    x = ob1;
    while(*x)
    {
        if(*x == ' ')
            putchar('\n');
        else
            putchar(*x);
        x++;
    }

    return(0);
}

The output of this code is that same as with the previous example. If I were making a choice between the two, I’d use the first example because more programmers — especially those just learning — would be more comfortable with it.

The next major change you can make is to manipulate the string directly in memory. That’s covered in next week’s Lesson.

Leave a Reply