Canna

Tuesday, 25 September 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.
  • Test Case 1

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

    Input (stdin)
    asdfghjk
    
    
    Expected Output
    kjhgfdsa
  • Program
  • #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;
    }
      

No comments:

Post a Comment

Note: only a member of this blog may post a comment.