-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path02_swap_two_strings.c
60 lines (47 loc) · 1.61 KB
/
02_swap_two_strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// // Write a function to swap values of two in variables by calling a function. (TSRN)
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define ARRAY_SIZE 31
// // Functions Declarations (Prototypes)
void swapTwoStrs1(char *, char *);
void swapTwoStrs2(char **, char **);
// // Main Function Start
int main()
{
char str1[ARRAY_SIZE], str2[ARRAY_SIZE];
printf("\nEnter String-1 (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
fgets(str1, ARRAY_SIZE, stdin); // // Input String
str1[strcspn(str1, "\n")] = '\0'; // // Replace '\n' character with '\0'
printf("\nEnter String-2 (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
fgets(str2, ARRAY_SIZE, stdin); // // Input String
str2[strcspn(str2, "\n")] = '\0'; // // Replace '\n' character with '\0'
printf("\n\n>>>>>>>>>>> Before Swapping <<<<<<<<<<<\n");
printf("str1 => %s\nstr2 => %s\n", str1, str2);
// // Swap Strings str1 and str2 (1st Approach)
swapTwoStrs1(str1, str2);
// // Swap Strings str1 and str2 (2nd Approach)
// // swapTwoStrs2((char **)&str1, (char **)&str2);
printf("\n\n>>>>>>>>>>> After Swapping <<<<<<<<<<<\n");
printf("str1 => %s\nstr2 => %s\n", str1, str2);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Function to Swap Two Strings (1st Approach)
void swapTwoStrs1(char *str1, char *str2)
{
char temp[ARRAY_SIZE];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
// // Function to Swap Two Strings (2nd Approach)
void swapTwoStrs2(char **str1, char **str2)
{
char *temp = *str1;
*str1 = *str2;
*str2 = temp;
}