简体   繁体   English

计算字符串中的子字符串字符

[英]Counting substring characters in string

I'm writing a function that takes an input string consisting of a mix 'o' 'g' and 'c'.我正在编写一个函数,它接受一个由“o”、“g”和“c”组成的输入字符串。 For each of these characters in the string, a certain symbol is printed.对于字符串中的每个字符,都会打印一个特定的符号。 However, for the 'g' character, id like to count how many g's are in a row in the string.但是,对于 'g' 字符,id 喜欢计算字符串中一行中有多少个 g。 Here is the code I have so far:这是我到目前为止的代码:

void printSymbol(char *str) // takes
{
  int i; // counter for entire string
  int k; // counter for substring ('g' char)
  int count = 0; //stores amount of consecutive g's in substring

  for (i = 0; str[i] != '\0'; i++) //looping until null term.
  {
    if (str[i] == 'o')
      printf("==========\n"); //symbol for o
    if (str[i] == 'c')
      printf(" ~~~~~~~~ \n"); //symbol for c
    if (str[i] == 'g') //if character is g
    {
      for(k = i; str[k] == 'g'; k++) //will loop through string starting at first g until next character is NOT g
      {
        count++; //count amount of consecutive g
      }
      printf("%d\n", count); //printing number of g's in a row
      if (count % 3 == 2) //All of these if statements are printing.
        printf("R==2\n");
      if (count % 3 == 1)
        printf("R == 1\n");
      if (count % 3 == 0)
        printf("R == 0\n");

      count = 0; // resets count for next set of consecutive g's in string
    }
  }
}

The problem I'm having is that it is printing all if statements in the for loop for the g char.我遇到的问题是它正在为 g 字符打印 for 循环中的所有 if 语句。 If there are 3 g's in a row, it will print R = 0, then R = 2, and R = 1, because it will loop through each g and thus the remainder is changing.如果一行中有 3 个 g,它将打印 R = 0,然后 R = 2 和 R = 1,因为它将遍历每个 g,因此余数正在改变。 How do I stop this?我该如何阻止? Id like to record the remainder for the entire set of consecutive g's one time.我想记录整组连续 g 的余数一次。 So if there are 3 g's, Id like to print R = 0, and then reset the counter var.所以如果有 3 个 g,我想打印 R = 0,然后重置计数器 var。

You are not updating your i variable when checking for consecutive g characters.检查连续 g 字符时,您没有更新i变量。 Your inner for loop that checks for consecutive g characters uses a separate loop variable, k , but since the i variable (the main loop counter) is not updated, then the string of consecutive g characters would be checked multiple times.检查连续 g 字符的内部for循环使用单独的循环变量k ,但由于i变量(主循环计数器)未更新,因此将多次检查连续 g 字符的字符串。

Eg例如

Assume your string is "...ggg...".假设您的字符串是“...ggg...”。 Then the first time you encounter this string, the inner for loop will check for the number of g characters and find that it is 3. It will then print out R == 0 , reset the count to 0 and go back to the main for loop.那么第一次遇到这个字符串时,内部for循环会检查g个字符的个数,发现是3个,然后打印出R == 0 ,将count重置为0,回到main for环形。

However, the outer for loop would then see the string as "gg..." and it will once again enter the inner for loop.但是,外部for循环会将字符串视为“gg...”,它将再次进入内部for循环。 This time, count would be 2 and R == 2 would be printed out.这一次, count将是 2 并且R == 2将被打印出来。

This process would then repeat for the final time.这个过程会在最后一次重复。 The string to be processed is "g...".要处理的字符串是“g...”。 This time, the count would be 1 and R == 1 would get printed out.这一次, count将是 1 并且R == 1将被打印出来。

To solve this, you simply have to update your i variable by adding the number of g characters you found - 1 (since the loop counter would increment at the end of the loop).要解决此问题,您只需通过添加您找到的 g 字符数 - 1 来更新您的i变量(因为循环计数器会在循环结束时增加)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM