EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

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;
10    printf("\nEnter first string:");
11    gets(str1);
12    printf("\nEnter second string:");
13    gets(str2);
14    while(str1[l1]!='\0')
15    {
16      l1++;
17    }
18    while(str2[l2]!='\0')
19    {
20      l2++;
21    }
22    while(str1[l]!='\0' || str2[l]!='\0')
23    {
24         t=str1[l];
25         str1[l]=str2[l];
26         str2[l]=t;
27         l++;
28    }
29    if(l==l1)
30    {
31      for(i=l;i<=l2;i++)
32      {
33            str1[i]=str2[i];
34      }
35      str1[l2+1]='\0';
36    }
37    else
38    {
39      for(i=l;i<=l1;i++)
40      {
41            str2[i]=str1[i];
42      }
43      str2[l1+1]='\0';
44    }
45    printf("\nThe first string is:");
46    puts(str1);
47    printf("\nThe second string is:");
48    puts(str2);
49    getch();
50  }

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