在选择的行中计算字符的实例数。

huangapple go评论82阅读模式
英文:

Counting instances of a character in select lines

问题

Currently learning c++ and I'm pretty stumped. I want to count the instances of a character in a text file - but not including lines that start with a certain character. Specifically, I'm counting instances of Gs and Cs in a text file, but not including lines that begin with "*"

Example:
*metadata information
atgctaatgcaggtcagtcagtcagtcatgcg
atgcagtcagtcactgactgactgactgaata
*metadata information
atgtagcagctagtcagtcagtcagcatatat
gatcgactagctgactgacgtactgactgaat

  1. char Z;
  2. long GC=0;
  3. string Line;
  4. while(getline(InFile, Line))
  5. {
  6. if(Line[0]=='*')
  7. {
  8. InFile.get(Z);
  9. while(InFile.get(Z))
  10. {
  11. if(Z=='G' || Z=='C' || Z=='g' || Z=='c')
  12. {
  13. ++GC;
  14. }
  15. }
  16. }
  17. }

I'm able to count the instances of G and C across the entire text, but just haven't been able to limit the function to lines that do not begin with '*'.

英文:

Currently learning c++ and I'm pretty stumped. I want to count the instances of a character in a text file - but not including lines that start with a certain character. Specifically, I'm counting instances of Gs and Cs in a text file, but not including lines that begin with "*"

Example<br/>
*metadata information<br/>
atgctaatgcaggtcagtcagtcagtcatgcg<br/>
atgcagtcagtcactgactgactgactgaata<br/>
*metadata information<br/>
atgtagcagctagtcagtcagtcagcatatat<br/>
gatcgactagctgactgacgtactgactgaat<br/>

  1. char Z;
  2. long GC=0;
  3. string Line;
  4. while(getline(InFile, Line))
  5. {
  6. if(Line[0]==&#39;*&#39;)
  7. {
  8. InFile.get(Z);
  9. while(InFile.get(Z))
  10. {
  11. if(Z==&#39;G&#39; || Z==&#39;C&#39; || Z==&#39;g&#39; || Z==&#39;c&#39;)
  12. {
  13. ++GC;
  14. }
  15. }
  16. }
  17. }

I'm able to count the instances of g and c across the entire text, but just haven't been able to limit the function to lines that do not begin in >

答案1

得分: 2

My understanding of your requirements, you want to ignore lines starting with &#39;*&#39;.

在上面的代码中,如果第一行字符是&#39;*&#39;,则忽略该行。
否则,搜索字符串中的&#39;G&#39;&#39;C&#39;字符。

英文:

My understanding of your requirements, you want to ignore lines starting with &#39;*&#39;.

  1. while (getline(InFile, Line))
  2. {
  3. if (Line[0] == &#39;*&#39;)
  4. {
  5. continue; // ignore the line
  6. }
  7. for (int i = 0; i &lt; Line.length(); ++i)
  8. {
  9. const char c = std::toupper(Line[i]);
  10. if ((c == &#39;G&#39;) || (c == &#39;C`))
  11. {
  12. ++GC;
  13. }
  14. }
  15. }

In the above code, if the first line character is '*', the line is ignored.
Otherwise, the string is searched for 'G' or 'C' characters.

答案2

得分: 0

InFile.get(Z);
while (InFile.get(Z))

你不想要这些行。在你的代码中,整个字符串已经被读入string Line;

你可能想要

for (auto c : Line) // 遍历Line中的每个字符
{

而且你可能想修正:

if (Line[0] != '*')

因为

但不包括以特定字符开头的行。

英文:
  1. InFile.get(Z);
  2. while(InFile.get(Z))

You don't want those lines. At this point in your code, the whole string has already been read in string Line;

You probably want

  1. for(auto c: Line) // go over every char in Line
  2. {

And you probably want to fix:

  1. if(Line[0] != &#39;*&#39;)

because

>but not including lines that start with a certain character.

huangapple
  • 本文由 发表于 2023年2月9日 01:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定