Friday, August 17, 2018

REVERSE THE STRING

  • Problem Description

    A and B are friends. They decide to play a game which is one person is writing a group of words or numbers to form a sentence and other person has to reverse each word of that sentence.
  • CODING ARENA
  • #include <stdio.h>
    #include<string.h>
    int main()
    {
      int i,j=0,k=0,len,x;
      char str[100],str1[10][20],temp;
      scanf("%[^\n]s",str);
      
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i] == ' ')
        {
          str1[k][j]='\0';
          k++;
          j=0;
        }
        else
        {
          str1[k][j]=str[i];
          j++;
        }
      }
      str1[k][j]='\0';
      
      for(i=0;i<=k;i++)
      {
        len=strlen(str1[i]);
        for(j=0,x= len - 1; j<x; j++, x--)
        {
          temp=str1[i][j];
          str1[i][j]=str1[i][x];
          str1[i][x]=temp;
        }
      }
      for(i=0;i<=k;i++)
      {
        printf("%s ",str1[i]);
      }
      return 0;
    }
  • Test Case 1

    Input (stdin)
    c programming world
    
    
    Expected Output
    c gnimmargorp dlrow
  • Test Case 2

    Input (stdin)
    abcd
    
    
    Expected Output
    dcba

1 comment: