• C program for swapping of two strings.
  • C program to swap two strings without using library functions.
  • Now we will swap two strings without using library functions and by using arrays in java.
  • Now we will write a C program on how to swap two strings.



Program #1 Write a C program to swap two strings


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.  int i=0,j=0,k=0;
  6.   char str1[20],str2[20],temp[20];
  7.   puts("Enter first string");
  8.   gets(str1);
  9.   puts("Enter second string");
  10.   gets(str2);
  11.   printf("Before swapping the strings \n");
  12.   puts(str1);
  13.   puts(str2);
  14.   while(str1[i]!='\0'){
  15.              temp[j++]=str1[i++];
  16.   }
  17.   temp[j]='\0';
  18.   i=0,j=0;
  19.   while(str2[i]!='\0'){
  20.               str1[j++]=str2[i++];
  21.   }
  22.   str1[j]='\0';
  23.   i=0,j=0;
  24.   while(temp[i]!='\0'){
  25.               str2[j++]=temp[i++];
  26.   }
  27.   str2[j]='\0';
  28.   printf("After swapping the strings are\n");
  29.   puts(str1);
  30.   puts(str2);
  31.   getch();
  32. }

  
Output:


swap strings in c program

Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu