I’m trying to write a program that finds strings in a character array and output the strings randomly as well as output string number and length. The trouble I seem to be having is picking out the length. I’m not sure if I should use the length stored in the comment (if it can even do that) or count each character and output the length that way. I’m not really sure how to do that though.
char * strings [] ={
// String 1 of 5000: Length = 185
"J1{GW3_%-3s_%p*E<ed<qLB#YHN%S8.odr5|[QPz&Hslk#3vi[)T3wgh3lHdVtTGz|M1RsGy_r=J]Rgp`0+s)pbvpm<u'8NsPX:Uk)kU,d5t@w[{2efjt*z_`eOqa#iP3z)T<(eYWb%W{5g?ynp*<jfEeLUA5:ukgvw$Le,Yjv*o{a/,tV#dG1|+D",
// String 2 of 5000: Length = 9
"^PuU]gjh)",
}
Right now I’m using a rand function to output 5,000 strings to an output file like so
outfile1 << strings[rand()%5000] << endl;
This function is nested within a loop and it seems to output it randomly, but it’s the same random pattern every time, so maybe I should seed the rand function differently.
Anyway, I really am lost when it comes to showing the length of each string in the output file. Does anyone have any suggestions as to how I can do this? This is homework so I’d like some information on how to apply this rather than a straight answer. 🙂
As of now my output is this:
#11 (Length:168) - JZn'TGF&#K=EohoZT
#12 (Length:189) - DiF9ao^T,7rtQ#Yc>n{_YIG_y
#13 (Length:50) - y,|l2;hA;H;pHz?|jLADh
As you can see, the strings output correctly but the length is incorrect.
///Updated answer//
I think what you want to do is to output each string in the array and calculate each of their length right??? I wrote this quick program and it worked fine.
You can replace cout with your output file string.
Make sure you use the same
kto get both the string and the length!////////////////////////////////////////////////
A character is a one-bit character such as ‘A’, ‘b’, ‘%’, etc.
A character string is simply an array of characters.
On the other hand, a string is a special class that represent a series of characters.
Character String, works like a normal array.
In your case, the
strings(which is a character array), you can just usesizeof(strings)/sizeof(strings[0])to find out the number of characters.As you mentioned, that character string is made up of many short strings. Unless there are some special characters that separate the words, there is no way (at least not easy way) to separate the string of characters into words.
For example if your character string is
hellothisistom. There is no way to separate the words… unless you go into natural language processing.If your character string is like
hello/this/is/tom, which there is some thing that separates individual words, then you can use many kinds of methods to extracts the words. You can check out the site below for the tools that you can use.http://www.cplusplus.com/reference/clibrary/cstring/
For example, in a loop you can search for the first occurrence of that special character and search for the one after that. Then you just copy the characters in between, output those characters and and size (using that method mentioned above). Then move on to the next one.
When you output
stings[rand()%5000], won’t there be just a bunch of random characters insidestrings?