LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 11-29-2009, 04:42 PM   #1
gerardorn
LQ Newbie
 
Registered: Nov 2009
Location: México
Posts: 9

Rep: Reputation: 0
Question Why i get a weird symbol at the end of a string?


hey

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.

here is what i get from runing the program

Code:
$ ./algoritmos lkjhgfdsa
pattern[1]: g
pattern[2]: ff
pattern[3]: kjf
pattern[4]: fhhg
pattern[5]: gsfks
pattern[6]: dghggj
pattern[7]: gajfgka
pattern[8]: jkhsslal
pattern[9]: dfhklaadl
pattern[10]: dggkllhgad
pattern[11]: lgsaslhfsgf
pattern[12]: skkjjjjjljag
pattern[13]: skgsfasjlhklk
pattern[14]: kgdlahakgkgdgd
pattern[15]: dddjfsdggdhdsda
pattern[16]: aalhdlhlafgkljds
pattern[17]: lhgjlhlgsdsfgfghg
pattern[18]: fskdkkdddasaffalkj
pattern[19]: lgjghahagagahlsgdad
pattern[20]: gdhhfjajjlhflasfjsak
pattern[21]: hadjjdlsglgasahhkhfgh
pattern[22]: llhaalkdahlalhkshlhgfh
pattern[23]: jgdddlhjhhjshjsgasaadah
pattern[24]: asdlkjfgflkjsjdldlhghfha
pattern[25]: gjsgassadfasaghgffdhshhfg
pattern[26]: skahgaadsgddhfhlgjalfhfjka
pattern[27]: lfhdkkslfjlglslsfhhahajjaad
pattern[28]: gksgjhaagkdfdlfjkgjalfhlljhh�
... so on
and i cant use those strings =/
any ideas?
 
Old 11-29-2009, 05:40 PM   #2
raju.mopidevi
Senior Member
 
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Blog Entries: 12

Rep: Reputation: 92
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.
 
Old 11-29-2009, 06:22 PM   #3
gerardorn
LQ Newbie
 
Registered: Nov 2009
Location: México
Posts: 9

Original Poster
Rep: Reputation: 0
hi thx for taking the time to answer

but i think thats not the problem =/

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.
 
Old 11-29-2009, 07:15 PM   #4
raju.mopidevi
Senior Member
 
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Blog Entries: 12

Rep: Reputation: 92
ok.
See the module "randchar"

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}.
 
2 members found this post helpful.
Old 11-29-2009, 07:26 PM   #5
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 293

Rep: Reputation: 47
You are not zero-terminating your string.
And if you do, your malloc() will be one byte short.

=> malloc 1 more byte, zero terminate your string.
 
1 members found this post helpful.
Old 11-30-2009, 03:57 PM   #6
gerardorn
LQ Newbie
 
Registered: Nov 2009
Location: México
Posts: 9

Original Poster
Rep: Reputation: 0
both of you were right
it works now

Code:
char randchar(const char *alphabet){
        int val;
        float mod;

        mod =(float) strlen(alphabet);
        val = (int)(mod * rand() / RAND_MAX);  
 return alphabet[val];
}
this is how it looks now and it works all good

Code:
char *randword(const char *alphabet, const int wlength){
        char *word;
        int i = 0;

        word = (char *)malloc(sizeof(char) * wlength+1);
        if(!word){
                printf("!mem\n");
                exit(1);
        }
        while(i < wlength){
                word[i++] = randchar(alphabet);
                word[i] = '\0';
        }
 return word;
}
and this is with the zero char at the end

thx for your help

Last edited by gerardorn; 11-30-2009 at 03:58 PM.
 
Old 11-30-2009, 05:46 PM   #7
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 293

Rep: Reputation: 47
[QUOTE=gerardorn;3774852]
Code:
...

        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.
 
1 members found this post helpful.
Old 12-01-2009, 02:08 AM   #8
gerardorn
LQ Newbie
 
Registered: Nov 2009
Location: México
Posts: 9

Original Poster
Rep: Reputation: 0
ur right, i did this

Code:
         val = (int)(mod * RAND_MAX / RAND_MAX);
and its an endless loop =/, should i use a do...while(val == mod)? or what do you think?
 
Old 12-01-2009, 05:00 AM   #9
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 293

Rep: Reputation: 47
Quote:
Originally Posted by gerardorn View Post

and its an endless loop =/, should i use a do...while(val == mod)? or what do you think?
I'd say the random function was ok as it was (with the '+ 1.0').
 
  


Reply

Tags
string, symbol


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string with dollar symbol in url parameters in php stranger_6_7 Linux - General 9 09-03-2008 04:42 PM
Using Delimiters to get the end of the string champak Programming 3 12-16-2007 06:15 AM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 07:17 PM
remove the 'd' character from the end of string powah Programming 6 11-08-2007 07:00 AM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM


All times are GMT -5. The time now is 07:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration