#include "stdio.h"
#include "stdlib.h"
#include "string.h"
char * reverseWords(char * s){
char * start, * start2;
start = s;
while(*start != '\0'){
start ++;
}
-- start;
char s2[strlen(s)];
char * s2p = s2;
while(start > s){
while(*start == ' ') start --;
while(*start != ' ' && start >= s) start --;
start2 = start;
++start2;
while(*start2 != ' '){
*s2p++ = *start2++;
}
*s2p++ = ' ' ;
}
*s2p = '\0';
start = s;
s2p = s2;
while(*s2p!='\0') *start++ = *s2p++;
*start = '\0';
return s;
}
int main(int argc, char ** argv){
char s[] = "abc edf ghi lkm opq rst ";
printf("%s\n",reverseWords(s));
}
在 linux 机器上能编译运行正确 leetcode 提示==20==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000020 at pc 0x563392652f73 bp 0x7ffefb8e0c40 sp 0x7ffefb8e0c30 READ of size 1 at 0x602000000020 thread T0
我是不是太贱了?
1
MoYi123 2023-09-15 15:28:44 +08:00 3
2023 年了, 写 c/c++ 还不开 fsanitize 那确实
|
3
lts9165 2023-09-15 16:49:34 +08:00
你把第 15 行的代码改成 while(start>=s && *start != ' ') start --; 就可以了,先判定界限
|
5
zzzkkk OP c 这样搞 宁可学 rust
|
6
standchan 2023-09-15 23:45:00 +08:00
语言只是工具,这句话不知道今年重复了多少遍
|
7
zzzkkk OP 是 leetcode 题目设计得不好 函数只传一个指针 还开 sanitize
正常项目根本不可能这样设计 下面这样就开了 sanitize 也运行正常 ```c #include "stdio.h" #include "stdlib.h" #include "string.h" int main(int argc, char ** argv){ char s[25] = "abc edf ghi lkm opq rst "; char * start, * start2; start = s; while( *start != '\0'){ start ++; } -- start; char s2[strlen(s)+1]; char * s2p = s2; while(start > s){ while(*start == ' ' ) start --; while(start >= s && *start != ' ') start --; start2 = start; ++start2; while( *start2 != ' '){ *s2p++ = *start2++; } *s2p++ = ' ' ; } *s2p = '\0'; start = s; s2p = s2; while(*s2p!='\0') *start++ = *s2p++; *start = '\0'; printf("%s\n", s); } ``` |
8
ngloom 2023-09-16 10:34:37 +08:00
```c
char * reverseWords(char * s){ char * a= s; char* b = s; while (*b) { b ++; } while(a != b) { char c = *a; } } ``` |
9
ngloom 2023-09-16 10:37:54 +08:00
还没写完就误点回复了, 囧
印象里简单代码的做法是 一个指针指向头, 一个指针指向尾, 然后两个指针互换内容后接着相向移动, 直到相遇. |
11
yanqiyu 2023-09-17 05:47:57 +08:00
倒序扣单词并不困难啊,维护两个 index ,一个头部一个尾部,寻找空格和非空格的边界就完了,哪用得着这么多 while
这么写结构清晰很多,并且含义还容易理解 #include <stdbool.h> #include <stdio.h> #include <string.h> void print_word(const char *str, size_t start, size_t stop) { for (size_t i = start; i < stop; i++) { // there is sth like %.*s but for simplicity ... printf("%c", str[i]); } } int main(int argc, char **argv) { char test_str[] = " f test sentence here bla bla "; size_t len = strlen(test_str) - 1; size_t start = len; size_t stop = len; // flag just for proper trailing space ... bool is_first_word = true; for (size_t i = len; i > 0; i--) { if (test_str[i] == ' ' && test_str[i - 1] != ' ') { stop = i; } else if (test_str[i] != ' ' && test_str[i - 1] == ' ') { start = i; if (is_first_word) { is_first_word = false; } else { printf(" "); } print_word(test_str, start, stop); } } return 0; } |
12
yanqiyu 2023-09-17 06:08:04 +08:00
@yanqiyu 洗了个澡发现边界情况写错了,修了(狗头)
#include <stdbool.h> #include <stdio.h> #include <string.h> void print_word(const char *str, size_t start, size_t stop) { for (size_t i = start; i <= stop; i++) { // there is sth like %.*s but for simplicity ... printf("%c", str[i]); } } int main(int argc, char **argv) { char test_str[] = " f test sentence here bla bla "; size_t len = strlen(test_str) - 1; size_t start = len; size_t stop = len; // flag just for proper trailing space ... bool is_first_wold = true; for (int i = len; i >= 0; i--) { if (i != 0 && test_str[i] == ' ' && (test_str[i - 1] != ' ')) { stop = i - 1; } else if (test_str[i] != ' ' && (i == 0 || test_str[i - 1] == ' ')) { start = i; if (is_first_wold) { is_first_wold = false; } else { printf(" "); } print_word(test_str, start, stop); } } return 0; } |
13
zzzkkk OP @yanqiyu
你这样是清晰了很多 但还不算 要弄个函数 reverseWords 返回 char *指针 我刚才改了下你的代码 弄到 leetcode 还报错 除非把*s2p++ = ' ';的++去掉 但输出就不正确了 ```c char * reverseWords(char * s){ char * test_str = s; size_t len = strlen(test_str) - 1; size_t start = len; size_t stop = len; char * s2 = malloc(len + 1); char * s2p = s2; bool is_first_wold = true; for (int i = len; i >= 0; i--) { if (i != 0 && test_str[i] == ' ' && (test_str[i - 1] != ' ')) { stop = i - 1; } else if (test_str[i] != ' ' && (i == 0 || test_str[i - 1] == ' ')) { start = i; if (is_first_wold) { is_first_wold = false; } else { *s2p++ = ' '; } for ( int k = start; k <= stop; k ++){ *s2p++ = test_str[k]; } } } *s2p = '\0'; s2p = s2; char * s3 = s; while(*s2p != '\0') { *s3 ++ = *s2p++; } *s3 = '\0'; free(s2); return s; } ``` |
14
colom 2023-09-17 12:49:59 +08:00
C 做题太难了,C++写着也不舒服,可惜我对其它的不熟
|
16
zzzkkk OP @yanqiyu
所以说 把那个++去掉就行 但留着++结果才正确 fsanitize 是不是报了假阳性 |
18
weeei 2023-09-18 18:57:16 +08:00
是必须自己实现 strstr(' ') 吗?
|