Advertisement
Shailrshah

Swap Strings (Without string.h)

Apr 19th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     char a[100],b[100],temp[100];
  5.     int i,la,lb;
  6.     printf("Enter the first string: ");
  7.     fgets(a,sizeof(a),stdin);
  8.     printf("Enter the second string: ");
  9.     fgets(b,sizeof(b),stdin);
  10.     for(la=0;a[la]!='\0';la++);
  11.     for(lb=0;b[lb]!='\0';lb++);
  12.     for(i=0; i<lb; i++)
  13.         temp[i] = b[i];
  14.     for(i=0; i<la; i++)
  15.         b[i] = a[i];
  16.     for(i=0; i<lb; i++)
  17.         a[i] = temp[i];
  18.     a[lb-1]='\0';
  19.     b[la-1]='\0';
  20.     printf("String A is %s and String B is %s.",a,b);
  21. }
  22. //Output
  23. //Enter the first string: Hello
  24. //Enter the second string: Hi
  25. //String A is Hi and String B is Hello.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement