Solved

C Radnom String

Posted on 2003-11-26
6
263 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

747 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

11 Experts available now in Live!

Get 1:1 Help Now