Finding Number of White-spaces in a line

Problem Statement
Write a recursive function to return the number
of white space characters present in a string.
Use the prototype int white Spaces(char *).
a The white space characters are blank (' ')
#include<stdio.h>
int total_spaces(char temp[]);
int main()
{
    char name[20];
    int ans;
    printf("enter the string and press 'enter' in the end\n");
    scanf("%[^'\n']",name);
    printf("the number read is %s \n",name);
    ans=total_spaces(name);
    printf("%d whitespace found \n",ans);
    return 0;
}
int total_spaces(char name[])
{
  if(*name=='\0')
    {
      return 0;
    }
  else if(*name==' ')
    {
      return 1+total_spaces(name+1);
    }
  else
    {
      return total_spaces(name+1);
    }
}

No comments:

Post a Comment