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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: