C program to swap two strings


SUBMITTED BY: Guest

DATE: Jan. 21, 2014, 11:09 p.m.

FORMAT: Text only

SIZE: 691 Bytes

HITS: 1240

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. int main()
  5. {
  6. char first[100], second[100], *temp;
  7. printf("Enter the first string\n");
  8. gets(first);
  9. printf("Enter the second string\n");
  10. gets(second);
  11. printf("\nBefore Swapping\n");
  12. printf("First string: %s\n",first);
  13. printf("Second string: %s\n\n",second);
  14. temp = (char*)malloc(100);
  15. strcpy(temp,first);
  16. strcpy(first,second);
  17. strcpy(second,temp);
  18. printf("After Swapping\n");
  19. printf("First string: %s\n",first);
  20. printf("Second string: %s\n",second);
  21. return 0;

comments powered by Disqus