计算字符数组中一行的字符数
我正在尝试编写一个程序,该程序在字符数组中查找字符串并随机输出字符串以及输出字符串编号和长度。我似乎遇到的麻烦是选择长度。我不确定是否应该使用注释中存储的长度(如果它甚至可以做到这一点)或计算每个字符并以这种方式输出长度。但我不太确定该怎么做。
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)",
}
现在我正在使用 rand 函数将 5,000 个字符串输出到输出文件,如下所示
outfile1 << strings[rand()%5000] << endl;
该函数嵌套在一个循环中,似乎是随机输出的,但每次都是相同的随机模式,所以也许我应该为 rand 播种功能不同。
不管怎样,当谈到显示输出文件中每个字符串的长度时,我真的很迷失。有人对我如何做到这一点有任何建议吗?这是家庭作业,所以我想要一些有关如何应用它的信息,而不是直接的答案。 :)
截至目前,我的输出是这样的:
#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
如您所见,字符串输出正确,但长度不正确。
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'TGFK=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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
///更新答案//
我认为你想要做的是输出数组中的每个字符串并计算它们的每个长度,对吗???我编写了这个快速程序并且运行良好。
您可以将 cout 替换为输出文件字符串。
确保使用相同的
k
来获取字符串和长度!////////////////////////////////////////////////
A < em>字符是一位字符,例如“A”、“b”、“%”等。
字符串只是一个字符数组。
另一方面,字符串是表示一系列字符的特殊类。
字符串,像普通数组一样工作。
在您的情况下,
strings
(这是一个字符数组),您可以使用sizeof(strings)/sizeof(strings[0])
来找出人物。正如您提到的,该字符串是由许多短字符串组成的。除非有一些特殊字符来分隔单词,否则没有办法(至少不是简单的方法)将字符串分隔成单词。
例如,如果您的字符串是
hellothisistom
。没有办法将单词分开......除非你进入自然语言处理。如果你的字符串像
hello/this/is/tom
,其中有一些东西分隔各个单词,那么你可以使用多种方法来提取单词。您可以查看下面的网站,了解可以使用的工具。http://www.cplusplus.com/reference/clibrary/cstring/
例如,在循环中,您可以搜索该特殊字符的第一次出现并搜索其后的字符。然后,您只需复制之间的字符,输出这些字符和大小(使用上面提到的方法)。然后继续下一个。
当你输出
stings[rand()%5000]
时,strings
里面不就只有一堆随机字符吗?///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
k
to 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
?您最终要解决的问题是什么,您是否正在尝试解析数据?
我不明白这个“在字符数组中查找字符串并随机输出字符串”;您必须知道 char 数组可以被理解为一个巨大的字符串,所以您是否正在寻找特殊的分隔符并且想要返回所有有效匹配的列表?
如果是这样,您如何区分源中的有效字符串和随机内容;应该划定界限吗?
如何随机输出字符串?
请添加更多有关您要解决的问题和正在处理的数据的详细信息。
What's the problem that you'd ultimately solve, are you trying to parse data ?
I don't understand this 'finds strings in a character array and output the strings randomly'; You must know that a char array can be comprehended as a giant string, so are you looking for special delimiters and you want to return a list of all the valid matches ?
If so, how do you tell the difference between a valid string and random stuff in the source; should it be delimited ?
How can you output strings randomly ?
Please add some more detail about what you're trying to solve and the data you're working on.
str.size() 其中str是你的字符串变量将输出字符串的长度
str.size() where str is your string variable will output the length of the string
srand(时间(0));
生成合适的伪随机数的标准方法。 'srand' 根据函数中传递的数字指定种子,而 'time(0)' 每秒会返回不同的数字,因此这应该可以解决您的“相同模式”问题。
a) 您可以使用 strlen
b) 或者您可以简单地编写一个函数,该函数将遍历字符串检查每个字符是否为 0(0 - 行尾),然后返回当前迭代值。
The standard way to generate decent pseudo-random numbers. 'srand' specifies a seed based on the number passed in the function, while 'time(0)' will return different number every second, so this should fix your 'same pattern' issue.
a) You can use strlen
b) Or you can simply write a function that will go through the string checking each character for 0 (0 - end of the line) and then returning the current iteration value.