L
L
LittleFatNinja2015-06-24 20:42:41
C++ / C#
LittleFatNinja, 2015-06-24 20:42:41

How to add "prefix" to argv[1] argument in C?

How to add "prefix" to argv[1] argument in C?
A string comes into the program as the second argument, let's do "name".
How to add "my" to it?
I implemented the algorithm for shifting the array to the right, I shift the array and first add a prefix, but I need to know the length of the incoming array , which is the problem

void arrayShift(char array[], int len, int offset) {
  int last = len - 1;
  
  for ( int i = last; i >= 0; i-- ) {
    array[i+offset] = array[i];
  }
}

roughly the following is needed:
arrayShift(argv[1], size, 2);

argv[1][0] = 'm';
argv[1][1] = 'y';

prinf("%s\n", argv[1]);

help figure it out!!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
LittleFatNinja, 2015-06-24
@LittleFatNinja

in short, I just included string.h and took strlen()

E
Eddy_Em, 2015-06-24
@Eddy_Em

char *p = malloc(strlen(argv[2]+4));
sprintf(p, "my %s", argv[2]);
argv[2] = p;

A
Alexander Gusev, 2015-06-24
@Sanchogus

string.h library for C
function strlen( const char * string );
will return the number of characters in the string
, the strcat function for appending to the string
char * strcat( char * destptr, const char * srcptr );
those.
charbuf*="my";
strcat(buf,argv[1]);
argv[1]=buf;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question