Solved

C Radnom String

Posted on 2003-11-26
6
275 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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
static method overriding vs normal non static method overriding in child class 13 103
data scientists and AI 17 115
word0 challenge 3 101
Problem to Alipay 10 46
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.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

828 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