File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ int firstUniqChar (char * s ) {
2
+ int ascii [256 ]= {0 };
3
+ int i = 0 ,len ;
4
+ while (s [i ]!= '\0' )
5
+ {
6
+ i ++ ;
7
+ }
8
+ len = i ;
9
+ for (i = 0 ;i < len ;i ++ )
10
+ {
11
+ ascii [s [i ]]++ ;
12
+ }
13
+ i = 0 ;
14
+ while (ascii [s [i ]]!= 1 && i < len ) i ++ ;
15
+ if (i == len )
16
+ return -1 ;
17
+ return i ;
18
+ }
Original file line number Diff line number Diff line change
1
+ int top=0;
2
+ void push(char c,char* stack)
3
+ {
4
+ stack[ top++] =c;
5
+ }
6
+ char pop(char* stack)
7
+ {
8
+ return stack[ --top] ;//写入s1
9
+ }
10
+ void reverseWords(char * s) {
11
+ int i=0,j=0,flag=0;
12
+ while(s[ i] !='\0') i++;
13
+ char* stack = (char* )malloc(i);
14
+ char* s1 = (char* )malloc(i+1);
15
+ i--;
16
+ while(i>=0)
17
+ {
18
+ if(s[ i] ==' ')
19
+ {
20
+ while(top!=0)//如果栈不为空
21
+ {
22
+ flag=1;
23
+ s1[ j++] =pop(stack);
24
+ }
25
+ if(flag==1)
26
+ {
27
+ s1[ j++] =' ';
28
+ flag=0;//重置flag
29
+ }
30
+ i--;
31
+ }
32
+ else
33
+ {
34
+ push(s[ i--] ,stack);
35
+ }
36
+ }
37
+ if(s[ ++i] !=' ')
38
+ {
39
+ while(top!=0)//如果栈不为空
40
+ s1[ j++] =pop(stack);
41
+ s[ j] =' ';
42
+ }
43
+ else
44
+ j--;
45
+ s1[ j] ='\0';
46
+ s = strcpy(s,s1);
47
+ free(s1);
48
+ }
You can’t perform that action at this time.
0 commit comments