Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string.
For example,
countHi("xxhixx") → 1
Note:
Refer to the problem requirements.
Function specification:
int counthi(char *str)
Input and Output Format :
Input consists of a string. The output consists of an integer corresponding to the number of 'hi's in the string.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output.]
Sample Input and Output:
Enter the string
xxhixx
Count of hi = 1
Implementation of above program:
#include<stdio.h>
#include<stdlib.h>
int counthi(char *string)
{
if(*string=='\0')
{
return 0;
}
else
{
if(*string=='h'&&*(string+1)=='i')
return 1+counthi(string+2);
else
return counthi(string+1);
}
}
int main()
{
char string[50];
printf("Enter the string\n");
scanf("%s", string);
printf("Count of hi = %d\n", counthi(string));
return 0;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: