SWAPPING OF STRINGS USING C PROGRAM





Swapping of strings using c programming language


#include<stdio.h>
int main(){
  int i=0,j=0,k=0;
  char str1[20],str2[20],temp[20];
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before swaping the strings are\n");
  puts(str1);
  puts(str2);
  while(str1[i]!='\0'){
             temp[j++]=str1[i++];
  }
  temp[j]='\0';
  i=0,j=0;
  while(str2[i]!='\0'){
              str1[j++]=str2[i++];
  }
  str1[j]='\0';
  i=0,j=0;
  while(temp[i]!='\0'){
              str2[j++]=temp[i++];
  }
  str2[j]='\0';
  printf("After swaping the strings are\n");
  puts(str1);
  puts(str2);
  return 0;
}





11 comments:

Anonymous said...

it would be great if you can add algorithms and flowcharts to the programs....

mishthi mona said...

swaping of 2 functions

Anonymous said...

It doesn't work!!!

Anonymous said...

lol

Anonymous said...

It works perfectly

bamin said...

Hi
How can I do the same with the xor operator ?(without using third variable)

Unknown said...

temp[j]='\0' what does that mean?

Unknown said...

temp[j]='\0' what does that mean?

Unknown said...

#include
#include
int main()
{
char str1[10],str2[10],str3[10];
printf("before swapping:");
gets(str1);
gets(str2);
strcpy(str3,str1);
strcpy(str1,str2);
strcpy(str2,str3);
printf("after swapping\n");
puts(str1);
puts(str2);
}
/*OUTPUT:
before swapping:
abc
def
after swapping:
def
abc*/

Unknown said...

Tempriry string for using swapin g

Unknown said...

Good programmer