vendredi 4 septembre 2020

Looping through characters in String

so im looping through each character in this string inputted by the user. The string only consists of 'o' 'g' and 'c'. So for each character, I want to make a certain symbol print to the screen. I think I can do this by using if statements inside the loop but I'm a little confused on what goes inside the for loop: The following is real and sudo code bc idk what goes inside the sudo code yet, thus, this question:

So, first string is inputted by user: say occccgggooo. here's the function im working on:

 void printSymbol(char *str)
 {
   int i;
   counter = 0;
   for (i = 0; str[i] != '\0'; i++)
   { //sudo code begins
     if(o in string)
         printf("/*some symbol*/");
         counter++; //How do i incorporate a counter to move to next character?
     if(g in string)
     .......

And so on. I just don't know what goes inside each if statement to recognize each character in the string. Also, maybe instead of repeating each if statement i can create some function to call instead? should it just be if (str[i] = 'o') and so on? then have a counter variable move the loop forward?

Aucun commentaire:

Enregistrer un commentaire