-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_exchanger.c
57 lines (43 loc) · 1000 Bytes
/
string_exchanger.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
#include <stdio.h>
#define MAX 100
void exchange(char*, char*, int);
int main()
{
char s1[MAX];
char s2[MAX];
int maxLength = 0;
printf("Enter first string: ");
gets(s1);
printf("Enter second string: ");
gets(s2);
//checking which string has more length, that will be passed to exchange()
if(strlen(s1) > strlen(s2))
maxLength = strlen(s1);
else
maxLength = strlen(s2);
printf("First String was - ");
puts(s1);
printf("Second String was - ");
puts(s2);
exchange(s1, s2, maxLength);
printf("\n\n");
printf("First String is - ");
puts(s1);
printf("Second String is - ");
puts(s2);
return 0;
}
void exchange(char *s1, char *s2, int maxLength)
{
// s1 and s2 are just addresses !!!
int i = 0;
while(i != maxLength)
{
char temp = *s1;
*s1 = *s2;
*s2 = temp;
s1++;
s2++;
i++;
}
}