-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.cpp
92 lines (71 loc) · 1.86 KB
/
string.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<stdlib.h>
using namespace std;
int comp(char *str1,char *str2)
{
int i,j,k,a=0,b=0;
char rt,st;
for(i=0;*(str1+i)!='\0';i++);
int len =i;
for(i=0;*(str2+i)!='\0';i++);
cout << "i = " << i << endl << "len = " << len << endl;
if(len!=i)
return -1;
else{
for(i=len-1;i>=0;i--)
{
if(*(str1+i)!= *(str2+i))
{
//find where it is
for(j=i-1; *(str1+j) != *(str2+i) && j>=0;j--);
if(j<-1)
return -2;
else{
a+=1;
while(j<len){
for(k=0;k<len;k++)
{
if(k<j+1)
{
rt = *(str1+j+1);
*(str1+j+1) = *(str1+k);
*(str1+k) = rt;
}else if(k==j+1){
*(str1+k) = st;
}
cout << endl;
for(b=0;*(str1+b)!='\0';b++)
cout << *(str1+b);
cout <<endl << "At " << endl ;
}
j= j+1;
}
}
}
//cout << "Each round " << i;
}
}
return a;
}
int main()
{
char str1[100] = "hlole";//new char();//= (char * ) malloc(100 * sizeof(char));
char str2[100] = "hello";//new char();// = (char * ) malloc(100 * sizeof(char));
int i;
/*cout << "Reading string 1 : ";
for (i=0;*(str1+i-1)!= '\0';i++)
{
cin >> *(str1+i);
}
// gets(str1);
cout << "Read string 2 : ";
for (i=0;*(str2+i-1)!= '\0';i++)
{
cin >> *(str1+i);
}
*/
int result = comp(str1,str2);
cout<<endl << str1;
cout << "\n \n Result is : " << result << endl;
return 0;
}