Saturday 24 March 2012

Swap two string in C using third variable


/*Swap two string using third string*/
//swap one by one character using while loop



#include<stdio.h>
int main()
{
  int i=0,j=0;
  char String1[20],String2[20],Temp[20];
  puts("Enter first string");//can use "printf" also
  gets( String1 );//for read the input from user
  puts("Enter second string");
  gets( String2 );
  printf("Before swaping the strings are\n");
  puts( String1);
  puts( String2);
     while( String1 [i]!='\0')//loop till null character
      {
        Temp[j++]= String1[i++];
      } 

Temp[j]='\0';
   i=0,j=0;
  while( String2 i]!='\0')
      {
        String1[j++]= String2[i++];
      }
  String1[j]='\0';
  i=0,j=0;
  while(Temp[i]!='\0')
      {
       String1[j++]=Temp[i++];
      }
   String2[j]='\0';
  printf("After swaping the strings are\n");
  puts(str1);
  puts(str2);
  return 0;
}//end of main

No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}