-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone_way.cpp
More file actions
59 lines (49 loc) · 1.09 KB
/
one_way.cpp
File metadata and controls
59 lines (49 loc) · 1.09 KB
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
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
bool oneWay(string str1,string str2){
int l1 = str1.length();
int l2 = str2.length();
if( abs(l1-l2)>1 ){
return false;
}
int has_operator = 0;
int i = 0;
char* cstr1 = (char*) str1.c_str();
char* cstr2 = (char*) str2.c_str();
while( i <= min(l1,l2) ){
if(cstr1[i] != cstr2[i]){
if(has_operator==0){
if (l1>l2){
cout<< "first string: "<< str1 <<endl;
str1 = str1.substr (0, i) + str1.substr(i+1);
cout<< "new string: "<< str1 <<endl;
cout<< "second string: "<< str2 <<endl;
}
else if (l1<l2){
cout << "first string: " << str1 << endl;
cout << "second string: " << str2 << endl;
char letter = cstr2[i];
str1 = str1.substr(0,i) + letter + str1.substr(i);
cout<< "new string: " << str1 << endl;
}
has_operator = 1;
}
else{
return false;
}
}
else {
i = i+1;
}
}
return true;
}
int main(){
string string1 = "pale";
string string2 = "pami";
cout << oneWay(string1,string2) <<endl;
return 0;
}