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
}

Dani AI

Generated

You can avoid shifting altogether by generating the dash in place. Also fix a few issues first: give the function a name and return type, make the alphabet a single string literal, use rand() % (sizeof A - 1), and terminate with '\0' not 6. Seed rand() once in main.

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

void make_code(char *out, size_t cap) {
    static const char A[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
    if (cap < 8) return;                 /* needs "XXX-XXX" + '\0' */
    for (size_t j = 0; j < 7; ++j)
        out[j] = (j == 3) ? '-' : A[rand() % (sizeof A - 1)];
    out[7] = '\0';
}

int main(void) {
    srand((unsigned)time(NULL));
    char code[8];
    make_code(code, sizeof code);

    char url[128];
    snprintf(url, sizeof url, "https://example.com/?id=%s", code);
    puts(code);
    puts(url);
}

If you already have a 6-char string s and just want to display it as XXX-XXX, ’s formatting idea is solid; prefer snprintf with precision so you never read past either half:

snprintf(out, out_cap, "%.3s-%.3s", s, s + 3);

If you truly need to edit in place (per @Ancient Dragon’s notes about room), ensure capacity is at least len + 2 and move the tail including the terminator:

/* insert '-' at pos in s (length len, capacity cap) */
if (pos <= len && cap >= len + 2) {
    memmove(s + pos + 1, s + pos, len - pos + 1);  /* includes '\0' */
    s[pos] = '-';
}

Notes: - is URL-safe, so no escaping needed; if this ever identifies users, do not use rand() for security tokens.

Recommended Answers

All 6 Replies

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] = '-';

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

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
Member Avatar for Member #46692

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.