------------------------------------------------------------------------------
MC logo
Stupid Argument Tricks
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Command Line Arguments] [Stupid Argument Tricks]
strargs.cpp
/*
 * This program prints the arguments in vertical columns, filling the each one
 * before proceeding to the next.  It's really just a string exercise.
 */
#include <iostream>
#include <ctype.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
        // Skip the command name.  Boring.
        --argc; ++argv; 

        // How many columns to use?  See if the -c option is used.
        // Default is 3.
        int ncol = 3;
        if(argc >= 2 && string(argv[0]) == "-c") {
                // Convert the -c argument to an actual integer.
                ncol = atoi(argv[1]);

                // Skip the -c n 
                argc -= 2; argv += 2;
        }

        // Find the length of the longest one.
        int longest = 0;
        for(int m = 0; m < argc; ++m) {
                int len = string(argv[m]).length();
                if(len > longest) longest = len;
        }

        // Make an array of strings to represent the lines.  It's the number
        // of args divided by the number of columns, but we have to round
        // up if there's any remainder to account for the last partial line.
        int nlines = argc / ncol;
        if(argc % ncol > 0) ++nlines;
        string *lines = new string[nlines];
        // Note: nlines can be set in one statement as:
        //    int nlines = (argc + ncol - 1) / ncol
        // See if you can figure out why that works.

        // Build the lines.
        int line = 0;
        for(int m = 0; m < argc; ++m) {
                string arg = argv[m];

                // Pad out to the longest length.
                for(int n = longest - arg.length(); n--; )
                        arg += " ";

                // Add to the given line, and wrap the line index.
                if(lines[line] != "") arg = " " + arg;
                lines[line++] += arg;
                if(line >= nlines) line = 0;
        }

        // Print the lines.
        for(int m = 0; m < nlines; ++m)
                cout << lines[m] << endl;

        // Be tidy.
        delete [] lines;
}

You don't have to know a lot about plain C strings to use argv, since they will convert to C++ strings by casting or assignment.

Presume cs is a plain-C string, and str is a C++ string.

  1. Automatic conversion by assignment: str = cs;.
  2. If you prefer an explicit conversion, str = string(cs); or str = (string)cs;.
  3. You can also make a temporary conversion to run methods, like this: int n = string(cs).length(); or str = string(cs).substr(1,5);.