Been stuck on this for bloody ages. Seems like no easy way to do it!

Would appricate some help, thanks!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char (char *s, const int len) {
{
   static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                                  "123456789";
   for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = 6;
}

int main (void)
{
//print the random digits but in the middle need to insert - e.g
//FJ3-FKE
}

The easiest way to approach this is with sprintf

char buf[MAX_LEN];
sprintf( buf, "%s\-%s", &s[0], &s[3] );

You can also memcpy if you have enough space
[ Assuming s[] is fully initialized ( s[260] = { 0 }; ) ]

  char tmp[3];
  memcpy( tmp, &s[3], 3 );
  memcpy( &s[4], tmp, 3 );
  s[3] = '-';

Edited 3 Years Ago by Unimportant

How can I string that together? so I can use it in a web request?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char (char *s, const int len) {
{
   static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                  "123456789";
   for (int i = 0; i < len; ++i) {
        s[i] = alphanum[(sizeof(alphanum) - 6)];
    }

    s[len] = 0;
}

int main (void)
{
//print the random digits but in the middle need to insert - e.g
//FJ3-FKE
}

Is what I have so far

so I can use it in a web request?

How does that relate to the code you posted? Why would you want to put random characters in a web request (URL??)?

Use a temp buffer.
1. copy the first part of the string into the temp buffer
2. Add all the randome characters
3. Add the remainder of the original string

Edited 3 Years Ago by Ancient Dragon

Into a get request if you must know.

Tried mememove, will this actually work? >_<

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char (char *s, const int len) {
{
   static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                  "123456789";
   for (int i = 0; i < len; ++i) {
        s[i] = alphanum[(sizeof(alphanum) - 6)];
    }
    s[len] = 0;
    memmove(s+pos+1, s+pos, len-pos+3);
    s[pos]='-';
    puts(s);
}

int main (void)
{
//webrequeststuff
}

Tried mememove, will this actually work?

Not the way you coded it.

Whatever you do you need to make sure there is enough room in s to hold all the characters you want.

I think you will do better if you could show an example of what you are trying to do. Example:

Orignal string = "Hello World"
String to insert = "My"
Result = "Hello My World"

With that you can see you need to move "World" to the right in the array by the length of the "String to insert". So you will need three memmove's

  1. determine length of the string to be inserted.
  2. determine where to insert the new characters
  3. move all the characters from the position found in #2 above right by the number of characters in the string to be inserted.
  4. copy the new characters into the string

Edited 3 Years Ago by Ancient Dragon

Why even bother using c if you're intending on using this in a web request?

Javascript or better still Jquery would be quite apt here.

This article has been dead for over six months. Start a new discussion instead.