-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwithString.c
More file actions
194 lines (166 loc) · 2.7 KB
/
Copy pathwithString.c
File metadata and controls
194 lines (166 loc) · 2.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* withString.c
*
* Created on: 2015年8月3日
* Author: Song Zhi-Cheng
*/
#include "General.h"
int myAtoi(char* str){
int flags = 1;
int res = 0;
if(!str) return 0;
while(*str == ' ') str++;
if(*str == '+') {
str++;
if(*str == '-') return 0;
}
if(*str == '-') {
str++;
flags = -1*flags;
if(*str == '+') return 0;
}
while(*str>='0'&&*str<='9'){
res = res*10+*str-'0';
str++;
}
return res*flags;
}
char *myReverse(char* str){
if(!str) return NULL;
char tmp;
char* p = str;
char* q = str;
while(*q) q++;
q--; //key
while(q > p) {
tmp = *p;
*p = *q;
*q = tmp;
q--;
p++;
}
return str;
}
char* myItoA(int integer){
static char str[30] = {0};//key
int i = 0;
char flag = 0;
if(integer < 0){
flag = '-';
integer = integer*(-1);
}
while(integer != 0){
str[i++] = integer%10 + '0';
integer = integer/10;
}
str[i] = flag;
myReverse(str);
return str;
}
int searchSubstr(char* des,char* sub){
if(!des || !sub) return -1;
int count = 0;
char* ptr = sub;
while(*des){
if(*des == *sub && *sub!='\0'){
des++;
sub++;
}else{
des++;
sub=ptr;
}
if(!(*sub)){
count++;
sub=ptr;
}
}
return count;
}
/*
*
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
*/
void reverseWords(char *s) {
myReverse(s);
char* p = s;
char* q = s;
while(*q!='\0'){
if(*q == ' '){
char* p1 = p;
char* q1 = q-1;
while(p1 < q1){
char tmp = *p1;
*p1 = *q1;
*q1 = tmp;
q1--;
p1++;
}
p = q+1;
}q++;
}
char* p2 = p;
char* q2 = q-1;
while(p2 < q2){
char tmp = *p2;
*p2 = *q2;
*q2 = tmp;
q2--;
p2++;
}
}
int replaceBlank(char string[], int length) {
int i,j;
for(i = 0;i < length; i++) {
if(string[i] == ' ') {
for(j = length+2; j > i+2; j--) {
string[j] = string[j-2];
}
string[i] = '%';
string[i+1] = '2';
string[i+2] = '0';
length = length+2;
}
}
return length;
}
//最后一个单词的长度
int lengthOflastword(char* arr){
if(!arr) return -1;
int count = 0;
int newcount = 0;
while(*arr != '\0'){
if(*arr != ' ') count++;;
if(*arr == ' ' || *(arr+1) =='\0'){
newcount = count;
count = 0;
}
arr++;
}
return newcount;
}
//旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)
void rotateString(char* string,int offset){
if(!string) return;
int count = 0;
int i;
char *str = string;
while(*str != '\0') {
count++;
str++;
}
// if(offset > count) return;
if(offset == 0) return;
else{
while(offset){
char tmp = *(str-1);
for(i=count-1;i>0;i--){
string[i] = string[i-1];
}
string[0] = tmp;
offset--;
}
}
}