-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixstring.cpp
More file actions
68 lines (59 loc) · 1.08 KB
/
Copy pathfixstring.cpp
File metadata and controls
68 lines (59 loc) · 1.08 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
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
char* fix(char* s);
int main(int argc, char* argv[])
{
char* str(0);
if (argc > 0)
str = argv[1];
cout << str << endl;
cout << fix(str) << endl;
return 0;
}
size_t find_first_space(char* s, size_t N, size_t i)
{
while (i < N && s[i] != ' ') ++i;
return i;
}
size_t find_first_nospace(char* s, size_t N, size_t i)
{
while (i < N && s[i] == ' ') ++i;
return i;
}
void move(char* &s, size_t& N, size_t i, size_t j)
{
size_t n = j-i;
size_t d = n;
while(i+n < N)
{
s[i] = s[j];
i++; j++;
}
N -= d;
s[N] = '\0';
}
char* fix(char* s)
{
size_t N = strlen(s);
for (size_t i(0); i < N;)
{
size_t j = find_first_space(s,N,i);
if (j < N)
{
size_t k = find_first_nospace(s,N,j);
if ( k < N)
{
move(s,N,j+1,k);
i = j+1;
}
else{
break;
}
}
else
{
break;
}
}
return s;
}