Solved

C Radnom String

Posted on 2003-11-26
6
266 Views
Last Modified: 2010-08-05
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;
  srand((unsigned)time(NULL));
  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?
0
Comment
Question by:quirgle
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:quirgle
ID: 9825981
Oh, I should mention, it seems to seg fault on the stncat function.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9826046
@quirgle:

> 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,
Dex*
0
 
LVL 4

Accepted Solution

by:
brunomsilva earned 125 total points
ID: 9826161
before the for loop, type this:

output = (char*) malloc(loop);
strcpy(output,"");

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);

bye,
  bruno
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:quirgle
ID: 9826212
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++?
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9826238
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).

Dex*
0
 
LVL 4

Expert Comment

by:brunomsilva
ID: 9832530
dex,

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

cheers,
  bruno
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to I add a folder to an existing GIT repository? 7 113
has22 challenge 11 78
java ^ examples 8 62
EvenOdd challenge 10 88
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now