[SOLVED] Why i get a weird symbol at the end of a string?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
im trying to generate random strings of a given length with the alphabet that the user inputs from the command line.
Code:
char randchar(const char *alphabet){
int val;
float mod;
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / (RAND_MAX + 1.0));
return alphabet[val];
}
this function returns the char in the random index generated. the man-pages of rand gives this way of generating a random number within a range, in my case the range is the length of the alphabet.
Code:
char *randword(const char *alphabet, const int wlength){
char *word;
int i = 0;
word = (char *)malloc(sizeof(char) * wlength);
if(!word){
printf("!mem\n");
exit(1);
}
while(i < wlength)
word[i++] = randchar(alphabet);
return word;
}
so this function calls the randchar function and builds a word with length wlength. is made like this cus i wanted to build a big file (~900 mb) with a long string, like a dna string, and i have 1g ram.
Code:
for(i=1; i < 151; i++){
pattern = randword(argv[1], i);
printf("pattern[%d]: %s\n", i, pattern);
free(pattern);
}
this is from the main function.
anywaaays, when i try to generate random words with length 1, 2, 3, 4, ... 150, i get some strings with funy symbols at the end.
In the "for loop" you had given i=1 to 151 ......... see that.
ASCII charcter set consists of 128 charcters defined as 0 to 127
in those characters, 0-31,127 are the special symbols. which are like smiley symbols,shapes ..etc.
32-126 are charcters which you can see on the keyboard.
if you ask a charcterset no beyond 127, then it agian starts from 0 ..
i.e. 128 =0
129 =1
130 =2
131 =3
.
.
.
.
.
151 =21
make a change in your for loop to stop those symbols.
see, the loop is to make the word that long, it doesnt have anything to do with the ASCII code.
when the loop goes to 150 means: build a random word, with this alphabet, and make it 150 characters long
so it gets a random char from the alphabet and then appends it to a string until it has 150 chars, but this chars are all from the alphabet not randomized ASCII code.
char randchar(const char *alphabet){
int val;
float mod;
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / (RAND_MAX + 1.0));
return alphabet[val];
}
you said that range is length of alphabet. But you are adding 1 to it.
i.e.
if your alphabet is 9 char length. and if you want to generate a word of length 10. according to your code
the values of variable "val" will be ..{0,1,2,3,4,5,6,7,8,9,10}.
but the values of variable "val" must be ..{0,1,2,3,4,5,6,7,8,9}.
...
mod =(float) strlen(alphabet);
val = (int)(mod * rand() / RAND_MAX);
return alphabet[val];
Hmm. If rand() should return RAND_MAX, val will be equal to strlen(aphabet).
That means you would read past the end of the string. You would return the terminating zero.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.