strchr

How do I find the index of a character within a string in C?

狂风中的少年 提交于 2019-12-03 04:44:10
问题 Suppose I have a string "qwerty" and I wish to find the index position of the e character in it. (In this case the index would be 2 ) How do I do it in C? I found the strchr function but it returns a pointer to a character and not the index. 回答1: Just subtract the string address from what strchr returns: char *string = "qwerty"; char *e; int index; e = strchr(string, 'e'); index = (int)(e - string); Note that the result is zero based, so in above example it will be 2. 回答2: You can also use

why is strchr twice as fast as my simd code

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am learning SIMD and was curious to see whether it was possible to beat strchr at finding a character. It appears that strchr uses the same intrinsics but I assume that it checks for a null, whereas I know the character is in the array and plan on avoiding a null check. My code is: size_t N = 1e9; bool found = false; //Not really used ... size_t char_index1 = 0; size_t char_index2 = 0; char * str = malloc(N); memset(str,'a',N); __m256i char_match; __m256i str_simd; __m256i result; __m256i* pSrc1; int simd_mask; str[(size_t)5e8] = 'b'; char

C语言 strchr的实现

匿名 (未验证) 提交于 2019-12-02 23:32:01
#include<stdio.h> #include<stdlib.h> #include<string.h> char *mystrchr(char *str, char c) { int i; for (i = 0; i<strlen(str); i++) { if (*(str + i) == c) { return str + i; } } return NULL; } int main() { char *str = "abcdef"; char c = 's'; char *fin = mystrchr(str, c); printf("%s", fin); system("pause"); return 0; }

How do I find the index of a character within a string in C?

别说谁变了你拦得住时间么 提交于 2019-12-02 17:55:09
Suppose I have a string "qwerty" and I wish to find the index position of the e character in it. (In this case the index would be 2 ) How do I do it in C? I found the strchr function but it returns a pointer to a character and not the index. wj32 Just subtract the string address from what strchr returns: char *string = "qwerty"; char *e; int index; e = strchr(string, 'e'); index = (int)(e - string); Note that the result is zero based, so in above example it will be 2. You can also use strcspn(string, "e") but this may be much slower since it's able to handle searching for multiple possible

STRCHR vs STRRCHR difference? [closed]

我只是一个虾纸丫 提交于 2019-12-01 22:54:39
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I would like to know the difference between the two different uses. I believe the difference in some what very subtle. This is an

How does strchr implementation work

风流意气都作罢 提交于 2019-11-28 06:26:24
I tried to write my own implementation of the strchr() method. It now looks like this: char *mystrchr(const char *s, int c) { while (*s != (char) c) { if (!*s++) { return NULL; } } return (char *)s; } The last line originally was return s; But this didn't work because s is const. I found out that there needs to be this cast (char *), but I honestly don't know what I am doing there :( Can someone explain? I believe this is actually a flaw in the C Standard's definition of the strchr() function. (I'll be happy to be proven wrong.) (Replying to the comments, it's arguable whether it's really a

How does strchr implementation work

冷暖自知 提交于 2019-11-27 01:21:35
问题 I tried to write my own implementation of the strchr() method. It now looks like this: char *mystrchr(const char *s, int c) { while (*s != (char) c) { if (!*s++) { return NULL; } } return (char *)s; } The last line originally was return s; But this didn't work because s is const. I found out that there needs to be this cast (char *), but I honestly don't know what I am doing there :( Can someone explain? 回答1: I believe this is actually a flaw in the C Standard's definition of the strchr()