Is there a function in c that will return the index of a char in a char array?
c中是否有一个函数会返回char数组中的索引?
For example something like:
例如像:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
int index = findInedexOf( values, find );
6 个解决方案
#1
45
strchr
returns the pointer to the first occurrence, so to find the index, just take the offset with the starting pointer. For example:
strchr返回指向第一个事件的指针,因此要找到索引,只需使用起始指针的偏移量。例如:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
const char *ptr = strchr(values, find);
if(ptr) {
int index = ptr - values;
// do something
}
#2
5
int index = strchr(values,find)-values;
Note, that if there's no find
found, then strchr
returns NULL
, so index will be negative.
注意,如果没有找到,则strchr返回NULL,因此索引将为负。
#3
2
Safe index_of()
function that works even when it finds nothing (returns -1
in such case).
安全的index_of()函数,即使它没有发现任何东西(在这种情况下返回-1),它仍然有效。
#include <stddef.h>
#include <string.h>
ptrdiff_t index_of(const char *string, char search) {
const char *moved_string = strchr(string, search);
/* If not null, return the difference. */
if (moved_string) {
return moved_string - string;
}
/* Character not found. */
return -1;
}
#4
1
What about strpos?
大小写敏感呢?
#include <string.h>
int index;
...
index = strpos(values, find);
Note that strpos expects a zero-terminated string, which means you should add a '\0' at the end. If you can't do that, you're left with a manual loop and search.
注意,strpos希望得到一个零终止的字符串,这意味着您应该在末尾添加一个'\0'。如果你做不到这一点,你就只能进行手动循环和搜索。
#5
1
There's also size_t strcspn(const char *str, const char *set)
; it returns the index of the first occurence of the character in s
that is included in set
:
还有size_t strcspn(const char *str, const char *set);它返回集合中包含的s中第一次出现字符的索引:
size_t index = strcspn(values, "E");
#6
0
You can use strchr to get a pointer to the first occurrence and the subtract that (if not null) from the original char* to get the position.
您可以使用strchr获取指向第一个事件的指针,并从原始char*中减去该事件(如果不是null)以获取位置。
#1
45
strchr
returns the pointer to the first occurrence, so to find the index, just take the offset with the starting pointer. For example:
strchr返回指向第一个事件的指针,因此要找到索引,只需使用起始指针的偏移量。例如:
char values[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char find = 'E';
const char *ptr = strchr(values, find);
if(ptr) {
int index = ptr - values;
// do something
}
#2
5
int index = strchr(values,find)-values;
Note, that if there's no find
found, then strchr
returns NULL
, so index will be negative.
注意,如果没有找到,则strchr返回NULL,因此索引将为负。
#3
2
Safe index_of()
function that works even when it finds nothing (returns -1
in such case).
安全的index_of()函数,即使它没有发现任何东西(在这种情况下返回-1),它仍然有效。
#include <stddef.h>
#include <string.h>
ptrdiff_t index_of(const char *string, char search) {
const char *moved_string = strchr(string, search);
/* If not null, return the difference. */
if (moved_string) {
return moved_string - string;
}
/* Character not found. */
return -1;
}
#4
1
What about strpos?
大小写敏感呢?
#include <string.h>
int index;
...
index = strpos(values, find);
Note that strpos expects a zero-terminated string, which means you should add a '\0' at the end. If you can't do that, you're left with a manual loop and search.
注意,strpos希望得到一个零终止的字符串,这意味着您应该在末尾添加一个'\0'。如果你做不到这一点,你就只能进行手动循环和搜索。
#5
1
There's also size_t strcspn(const char *str, const char *set)
; it returns the index of the first occurence of the character in s
that is included in set
:
还有size_t strcspn(const char *str, const char *set);它返回集合中包含的s中第一次出现字符的索引:
size_t index = strcspn(values, "E");
#6
0
You can use strchr to get a pointer to the first occurrence and the subtract that (if not null) from the original char* to get the position.
您可以使用strchr获取指向第一个事件的指针,并从原始char*中减去该事件(如果不是null)以获取位置。