Archive for Coding

A cmd.exe sleep command?

SleepIt’s always a surprise when a batch file scripter realizes cmd.exe lacks a sleep command.  But fear not!  The choice command can be used as a substitute.  It lists a set of choices and waits a specified amount of time for the user to choose one.  Choice can be used to sleep for 0 to 9999 seconds (or over 2.7 hours).

Just create the following file and save it as sleep.cmd:

@echo off
set /a SleepTime = %1
CHOICE /N /D y /T %SleepTime% /M "Sleeping for %SleepTime% seconds . . ."


Place it in your aliases bin and you’re all set to sleep from the command line!

Leave a Comment

PWD for cmd.exe?

For years I’ve mourned the loss of my favorite unix shell command, pwd.  I can’t count the number of times I had typed those three  simple letters into my xterm window.  After moving to a Windows environment over 10 years ago I started anew with commands like dir instead of ls, type instead of more (wait! more works!), findstr instead of grep, etc…

But no replacement for pwd.  No worries, the prompt for cmd is the current working directory by default.  But what if you change the prompt to the date and time, like I often do (prompt $D$S$T$G), to have a history of when you types which command. That leaves you with no way to tell the current directory.

But today, I learned about the cd env variable. Echo’ing %cd% prints the current directory.  Excited by this, I created the simple batch file:

@echo off
echo %cd%\

I named it pwd.cmd and placed it in my aliases directory.


Leave a Comment

Reversing A String With C

The most common interview question I hear about is the problem involving reversing a string. There are countless methods to do so, many of which are so creative it makes you wonder how anyone came up with a solution. One aspect of the problem I’ve never heard discussed concerns the method used to declare the string. The answer can vary depending on if the string the function is reversing is a char* or a char array[]. Char arrays can be manipulated character by character whereas strings, char* in this case, are immutable.

Let’s begin with a solution that always works, regardless how the string is declared.

The function below, reverse1, takes a char* as input and returns a new pointer to a block of memory it will allocate with malloc. The function simply “reverse copies” the input string into the new string.

char* reverse1(char* myStr)
    int i, len;
    len = strlen(myStr);
    char* newStr = (char*)malloc((len+1)*sizeof(char));
    for (i=0; i<len; ++i)
        newStr[len-1-i] = myStr[i];
    newStr[len] = ‘\0’;

    return newStr;

The wonderful aspect of the above code is it will work on both char* strings and arrays of chars. All of the following strings can be passed as input:

// Arrays
char str1[] = “Hello”;
char str2[6] = “Hello”;
char str3[10];
printf(“Enter string> ”);
scanf(“%s”, str3);
char str4[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘’};

// strings
char* str5 = “Hello”;

However, one may want to perform the same reverse task without allocating new memory, or may infact wish to reverse the block of memory passed into the funtion instead of receiving back a pointer to a new block of memory. Here is another solution that accomplishes this.

If one attempts to pass in all the strings declared above, they will run into a problem.     This function attempts to get down and dirty with the string and rearrange the elements swapping the first element with the last and so on. The application will crash when passing in a pointer to a string (char*). Strings are immutable while arrays can be manipulated.

void reverse2(char* myStr)
    int i, len;
    char temp;
    len = strlen(myStr);
    for (i=0; i<len/2; ++i)
        temp = myStr[i];
        myStr[i] = myStr[len-1-i];
        myStr[len-1-i] = temp;
    myStr[len] = ‘’;

Leave a Comment