dmilicev

recursive_function_to_count_occurences_of_ch_in_string_v1.c

May 10th, 2020
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None
  1. /*
  2.  
  3.     recursive_function_to_count_occurences_of_ch_in_string_v1.c
  4.  
  5.     Using recursive function to count occurences of ch in string
  6.  
  7. https://stackoverflow.com/questions/40888686/using-recursive-function-to-count-occurences-of-letters?fbclid=IwAR0MO7rGj7jw7dmBQDF2okDeJZpavzKnHTnNmkR0vv6RTgY-H_esbYJjR_I
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. int count_ch_in_string( char *source, char ch )
  19. {
  20.     if(*source == '\0')
  21.         return 0;
  22.  
  23.     if( *source == ch )
  24.         return(1 + count_ch_in_string(++source, ch) );
  25.  
  26.     return( count_ch_in_string(++source, ch) );
  27. }
  28.  
  29.  
  30. int main(void)
  31. {
  32.     char str[]="This is a string to test recursive function count_ch_in_string().";
  33.     char ch='t';
  34.  
  35.     printf("\n In string: \n\n|%s| \n\n there are %d characters '%c' . \n\n",
  36.             str, count_ch_in_string(str,ch), ch);
  37.  
  38.  
  39.     return 0;
  40.  
  41. } // main()
RAW Paste Data Copied