Advertisement
paulogp

Ocorrencia

Aug 13th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3.  
  4. /* ocorrencia: numero de vezes que um caracter aparece numa palavra */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. int ocorrencia (char *the_input, char the_char);
  9.  
  10. int ocorrencia (char *the_input, char the_char)
  11. {
  12.     int i = 0;
  13.     int the_counter = 0;
  14.    
  15.     for (i = the_counter = 0; the_input[i] != '\0'; i++)
  16.     {
  17.         if (the_input[i] == the_char)
  18.         {
  19.             the_counter++;
  20.         }
  21.     }
  22.    
  23.     return the_counter;
  24. }
  25.  
  26. int main (int argc, const char * argv[])
  27. {
  28.     char the_input[21]; // max 20 digits + '\0'
  29.     char the_char[2]; // char to search + '\0'
  30.     int the_result;
  31.    
  32.     printf ("palavra: ");
  33.     fgets (the_input, sizeof(the_input) + 1, stdin);
  34.    
  35.     printf ("letra: ");
  36.     fgets (the_char, sizeof(the_char), stdin);
  37.    
  38.     the_result = ocorrencia (the_input, the_char[0]);
  39.     printf ("%c aparece %i vezes\n", the_char[0], the_result);
  40.    
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement