forked from wangcy6/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path151.reverse-words-in-a-string.c
70 lines (59 loc) · 1.06 KB
/
151.reverse-words-in-a-string.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
58
59
60
61
62
63
64
65
66
67
68
69
70
// CPP program to reverse a string
#include <stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
void reverse(char* begin, char* end);
/*Function to reverse words*/
void reverseWords(char* s)
{
char* start=s;
char* end =s+strlen(s);
//step01 trim
while(start &&*start == '' )
{
start++;
}
if (!start)
{
return "";
}
while(end||*end == '' )
{
end --;
}
if (!end)
{
return "";
}
if(start=s ||end =s+strlen(s) )
{
}else
{
strncpy(s,s+start,end-start);
}
//02 交换
while(end>start)
{
}
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char* begin, char* end)
{
char temp;
while (begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/* Driver function to test above functions */
int main()
{
char s[] = "i like this program very much";
char* temp = s;
reverseWords(s);
printf("%s", s);
return 0;
}