Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define SIZE 20
  5. char* GetSameChars(char* str1, char* str2);
  6.  
  7. int main()
  8. {
  9. char *string1, *string2;
  10. puts(GetSameChars(&string1, &string2));
  11. system("pause");
  12.  
  13. }
  14. char* GetSameChars(char* str1, char* str2)
  15. {
  16. char buffer1[SIZE], buffer2[SIZE];
  17.  
  18. printf("Enter array number 1:\n");
  19. gets_s(buffer1, SIZE);
  20. str1 = (char*)malloc(strlen(buffer1) + 1);
  21. strcpy_s(str1,SIZE, buffer1);
  22.  
  23. printf("Enter array number 2:\n");
  24. gets_s(buffer2, SIZE);
  25. str2 = (char*)malloc(strlen(buffer2) + 1);
  26. strcpy_s(str2, SIZE, buffer2);
  27.  
  28. int i = 0;
  29. int j = 0;
  30. int k = 0;
  31. char *arr=0;
  32. while (1)
  33. {
  34. if (str1[i] == str2[j])
  35. {
  36. arr[k] = str2[j];
  37. k++;
  38. j++;
  39. }
  40. else
  41. i++;
  42. if (i == strlen(str1) || j == strlen(str2));
  43. break;
  44. }
  45. arr=(char*)malloc(k*sizeof(char));
  46.  
  47. return arr;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement