Question

Write a recursive function that uses the strchr() function to display the number of occurrences of...

Write a recursive function that uses the strchr() function to display the number of occurrences of a character inside a string. Create a C program to read a string of less than 10 characters and a character and calls the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>

int strchr1(char* s, char ch){
   if(s == NULL || s[0] == '\0'){
      return 0;
   }
   else{
      if(s[0] == ch){
         return 1 + strchr1(s+1,ch);
      }
      else{
         return strchr1(s+1,ch);
      }
   }
}

int main (){
   char* s = (char*)malloc(sizeof(char)*11);
   char ch;
   printf("Enter string: ");
   scanf("%s",&s);
   
   printf("Enter char: ");
   scanf("%c",&ch);
   
   
   printf("Count = %d\n",strchr1(s,ch));
}

\color{blue}Please\; up\;vote\;the \;solution \;if \;it \;helped.\;Thanks!

\color{red}Note: \;Please \;comment \;below \;if \;you \;have \;any \;issues \;with \;this \;solution.\;

Add a comment
Know the answer?
Add Answer to:
Write a recursive function that uses the strchr() function to display the number of occurrences of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT