Finding Last Letter of a Line by Recursion

Problem Statement
Write a recursive function to return the last
character of a string. Use the prototype char
lastChar(char *). The last character of "IIT
Kharagpur" is 'r'.
#include<stdio.h>
char last(char temp[]);
int main()
{
  char name[20],last_char;
  printf("enter the string nad press 'enter'in hte end \n");
  scanf("%[^'\n']",name);
  printf("red data=%s \n",name);
  last_char=last(name);
  printf("%c is the last char \n",last_char);
  return 0;
}
char last(char temp[])
{
  if(*(temp+1)=='\0')
    {
      return *temp;
    }
  else
    {
      return last(temp+1);
    }
}

No comments:

Post a Comment