I'm writing a function that takes an input string consisting of a mix 'o' 'g' and '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. 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. 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. How do I stop this? Id like to record the remainder for the entire set of consecutive g's one time. So if there are 3 g's, Id like to print R = 0, and then reset the counter var.
Aucun commentaire:
Enregistrer un commentaire