-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_swapping.cpp
executable file
·59 lines (55 loc) · 1.15 KB
/
string_swapping.cpp
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
// string_swapping.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "malloc.h"
void swap_strings(char *string1,char *string2)
{
int i=0,flag=0;
char temp;
while(1)
{
if(string1[i]=='\0'&&string2[i]=='\0')
break;
if(string1[i]!='\0'&&string2[i]!='\0')
{
temp=string1[i];
string1[i]=string2[i];
string2[i++]=temp;
}
else if(string2[i]=='\0')
{
if(flag!=2){
flag=2;
string2[i]=string1[i];
string1[i++]='\0';
}
else
string2[i]=string1[i++];
}
else if(string1[i]=='\0')
{
if(flag!=1)
{
flag=1;
string1[i]=string2[i];
string2[i++]='\0';
}
else
string1[i]=string2[i++];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int max_len,i;
char *string1,*string2;
printf("enter the max length of the two strings\n");
scanf("%d",&max_len);
string1=(char*)malloc(max_len*sizeof(char));
string2=(char*)malloc(max_len*sizeof(char));
printf("enter the two strings to be swapped\n");
scanf("%s %s",string1,string2);
swap_strings(string1,string2);
printf("%s,%s",string1,string2);
return 0;
}