Skip to content
Snippets Groups Projects
Commit 1e969afd authored by Andrii Zymohliad's avatar Andrii Zymohliad
Browse files

Implement strpbrk(), add strpbrk test

parent a1de0ef8
No related branches found
No related tags found
1 merge request!75Implement strstr() and strpbrk() from string.h
......@@ -250,8 +250,15 @@ pub unsafe extern "C" fn strncpy(s1: *mut c_char, s2: *const c_char, n: usize) -
}
#[no_mangle]
pub extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char {
unimplemented!();
pub unsafe extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char {
let mut i = 0;
while *s1.offset(i) != 0 {
if !strchr(s2, *s1.offset(i) as i32).is_null() {
return s1.offset(i) as *mut c_char;
}
i += 1;
}
ptr::null_mut()
}
#[no_mangle]
......
......@@ -28,6 +28,7 @@ EXPECT_BINS=\
string/strrchr \
string/strspn \
string/strstr \
string/strpbrk \
unlink \
write
......
The quick drawn fix jumps over the lazy bug
lazy bug
NULL
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char* str = "The quick drawn fix jumps over the lazy bug";
// should be "The quick drawn fix jumps over the lazy bug"
char* str1 = strpbrk(str, "From The Very Beginning");
printf("%s\n", (str1) ? str1 : "NULL");
// should be "lazy bug"
char* str2 = strpbrk(str, "lzbg");
printf("%s\n", (str2) ? str2 : "NULL");
// should be "NULL"
char* str3 = strpbrk(str, "404");
printf("%s\n", (str3) ? str3 : "NULL");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment