Write a program to swap the two given strings without using string function

Source Code

#include<stdio.h>
#include<conio.h>
int main (int argc, char *argv[])
{
  clrscr();
  char str1[100];
  char str2[100];
  char t;
  int l1=0,l2=0,l=0,i;
  printf("\nEnter first string:");
  gets(str1);
  printf("\nEnter second string:");
  gets(str2);
  while(str1[l1]!='\0')
  {
    l1++;
  }
  while(str2[l2]!='\0')
  {
    l2++;
  }
  while(str1[l]!='\0' || str2[l]!='\0')
  {
       t=str1[l];
       str1[l]=str2[l];
       str2[l]=t;
       l++;
  }
  if(l==l1)
  {
    for(i=l;i<=l2;i++)
    {
          str1[i]=str2[i];
    }
    str1[l2+1]='\0';
  }
  else
  {
    for(i=l;i<=l1;i++)
    {
          str2[i]=str1[i];
    }
    str2[l1+1]='\0';
  }
  printf("\nThe first string is:");
  puts(str1);
  printf("\nThe second string is:");
  puts(str2);
  getch();
}

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code