Saturday, August 18, 2018

POINTERS-48


  • Problem Description

    Write a program to reverse a string using pointer

    Input and Output Format:

    Refer sample input and output for formatting specification.

    All float values are displayed correct to 2 decimal places.

    All text in bold corresponds to input and the rest corresponds to output.
  • CODING ARENA
  • #include <stdio.h>
    #include<string.h>
    int main()
    {
     char str[50];
      char rev[50];
      char *sptr=str;
      char *rptr=rev;
      int i=-1;
      scanf("%[^\n]s",str);
      while(*sptr)
      {
        sptr++;
        i++;
      }
      while(i>=0)
      {
        sptr--;
        *rptr=*sptr;
        rptr++;
        --i;
      }
      *rptr='\0';
      rptr=rev;
      while(*rptr)
      {
      *sptr=*rptr;
      sptr++;
      rptr++;
      }
      printf("%s",str);
      return 0;
    }
      
      /* char s1[20];
      char *s=s1;
      int len,i;
      scanf("%c",s1);
        len=strlen(s);
      for(i=len;i>0;i--)
        printf("%c",*(s+i));
      return 0;
    }
      
        
        
      
      
        
  • Test Case 1

    Input (stdin)
    SRM university
    
    
    Expected Output
    ytisrevinu MRS
  • Test Case 2

    Input (stdin)
    madam
    
    
    Expected Output
    madam

No comments:

Post a Comment