2
votes

I have a command line argument which is alphanumeric, e.g. "ABCDE1234567" passed as argv[1]. I'm looking to rearrange the char* so the first 4 chars are sent to the end of the string, so it would end up like E1234567ABCD.

I'm getting the char* from the argv as follows:

char* input = argv[1];
validate(input);

then passing to a function to do the work,

void validate (char* input) {
    // move the first 4 chars to the end
    // this is where i'm stuck :-)
}

I know this should be easy, but I've been trying to use strcpy, sprintf, for loops, but just not getting anywhere.

edit:

I tried to copy the string without the first four chars into a buffer, but this didnt seem to produce any results:

int n = strlen(input);
char buffer [n - 4];

for (int i = 4; i <= n; i++) {
    printf("%c\n", input[i]);
    sprintf(buffer, "%c", input[i]);
}
printf("%s\n", buffer);
3
Please include that problematic code in your question.Lee Duhem
What did you try yourself?ouah

3 Answers

2
votes

A combination of the string functions is probably the easiest choice for this:

#include <stdio.h>
#include <string.h>

void validate (char* input) {
    // Temp storage for four characters being moved.

    char four[4];

    // Get length but do nothing unless long enough.

    size_t len = strlen (input);
    if (len < 5)
        return;

    // Save first four characters.

    memcpy (four, input, 4);

    // Move the others (need memmove for overlapping regions).
    // input is address of first character.
    // &(input[n-1]) is address of nth character.

    memmove (input, &(input[4]), len - 4);

    // Put saved four characters at the end.
    // &(input[len - n]) is address of nth-last character.

    memcpy (&(input[len - 4]), four, 4);
}

// A simple test program for checking.

int main (int argc, char *argv[]) {
    if (argc < 2) {
        printf ("Not enough arguments\n");
        return 1;
    }
    printf ("Converted '%s' ", argv[1]);
    validate (argv[1]);
    printf ("to '%s'\n", argv[1]);
    return 0;
}
2
votes

Consider an alternative approach: what happens if you concatenate the two strings together:

ABCDE1234567 ABCDE1234567

...and then move a "sliding window" a few character forward:

ABCD E1234567ABCD E1234567

You can easily achieve this with a pointer to the right location and copy as much data as you need into a destination.

Beware that a string is an array of characters followed by a null char \0. A string is not simply an array of chars.

0
votes

below code might help.

void validate (char* input) {
        char temp[10];
        strncpy ( temp, input, 4 ); //copy first 4 chars to temp
        temp = '\0'; //terminate it.

        //Copy characters from 5th to end
        for(i=0;i<strlen(input)-4;i++)
         {
           input[i] = input[i+4];
         }
         //copy remaining four characters from temp at last
         for(i=strlen(input)-4,j=0;i<strlen(input);i++)
         {
           input[i]=temp[j];
           j++;
         }

}