C Radnom String

Ok, I need to generate some random lowercase strings with C. Here's what I've got so far, and I keep getting Seg Faults.

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

#define MIN_SIZE 5;
#define MAX_SIZE 20;

char * randstring()
  char c;
  char * output;
  int num, i, loop;
  double offset;
  offset = MAX_SIZE - MIN_SIZE;
  loop = MIN_SIZE+(int)(offset*rand()/(RAND_MAX+1.0));
  for (i = 0;i <= loop;i++) {
    num = 1+(int)(26.0*rand()/(RAND_MAX+1.0));
    num += 96;
    c = (char *)num;
    strcat(output, c);

  return output;

Any idea what I'm doing wrong?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quirgleAuthor Commented:
Oh, I should mention, it seems to seg fault on the stncat function.

> Any idea what I'm doing wrong?

You aren't allocating any memory.  Add these lines after the "loop = MIN_SIZE+..." line:
     output = new char[loop + 1];
     output[0] = (char)0;

Hope That Helps,
before the for loop, type this:

output = (char*) malloc(loop);

and to add the character to the string i advise using sprintf instead of strcat as strcat is designed for strings:

sprintf(output,"%s%c", output, c);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

quirgleAuthor Commented:
Thanx a lot bruno, works great. I still have much to learn in the C world.

Dex, it didn't understand 'new' char...   Isn't that C++?
Oops.  I forgot that new was C++.  In, C you should use malloc.

Brunomsilva's answer isn't 100% right though.   It should be:
     output = (char*) malloc(loop+1);
     output[0] = (char)0;

(His strcpy will work too, I guess).


you are right, i needed an extra byte to store the \0. i forgot that.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.