<p>Is there a C library function that will return the index of a character in a string?</p>

<p>So far, all I've found are functions like strstr that will return the found char *, not it's location in the original string.</p>

[{'answer_id': 4829, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 0, 'selected': False, 'text': '<p>If you are not totally tied to pure C and can use string.h there is strchr()\n<a href="http://www.cplusplus.com/reference/clibrary/cstring/strchr.html" rel="nofollow noreferrer">See here</a></p>\n'}, {'answer_id': 4832, 'author': 'Bill', 'author_id': 102, 'author_profile': 'https://Stackoverflow.com/users/102', 'pm_score': 5, 'selected': False, 'text': '<p><code>strstr</code> returns a pointer to the found character, so you could use pointer arithmetic:  (Note: this code not tested for its ability to compile, it\'s one step away from pseudocode.)</p>\n\n<pre><code>char * source = "test string";         /* assume source address is */\n                                       /* 0x10 for example */\nchar * found = strstr( source, "in" ); /* should return 0x18 */\nif (found != NULL)                     /* strstr returns NULL if item not found */\n{\n  int index = found - source;          /* index is 8 */\n                                       /* source[8] gets you "i" */\n}\n</code></pre>\n'}, {'answer_id': 4833, 'author': 'Michal Sznajder', 'author_id': 501, 'author_profile': 'https://Stackoverflow.com/users/501', 'pm_score': 4, 'selected': False, 'text': '<p>EDIT: strchr is better only for one char. \nPointer aritmetics says "Hellow!":</p>\n\n<pre><code>char *pos = strchr (myString, \'#\');\nint pos = pos ? pos - myString : -1;\n</code></pre>\n\n<p><strong>Important:</strong> strchr () returns NULL if no string is found</p>\n'}, {'answer_id': 4834, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>You can use strstr to accomplish what you want. Example:</p>\n\n<pre><code>char *a = "Hello World!";\nchar *b = strstr(a, "World");\n\nint position = b - a;\n\nprintf("the offset is %i\\n", position);\n</code></pre>\n\n<p>This produces the result:</p>\n\n<pre><code>the offset is 6\n</code></pre>\n'}, {'answer_id': 4838, 'author': 'Jonathan Works', 'author_id': 309844, 'author_profile': 'https://Stackoverflow.com/users/309844', 'pm_score': 5, 'selected': True, 'text': '<p>I think that</p>\n\n<blockquote>\n  <p>size_t strcspn ( const char * str1, const char * str2 );</p>\n</blockquote>\n\n<p>is what you want. Here is an example pulled from <a href="http://www.cplusplus.com/reference/clibrary/cstring/strcspn.html" rel="noreferrer">here</a>:</p>\n\n<pre><code>/* strcspn example */\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\nint main ()\n{\n  char str[] = "fcba73";\n  char keys[] = "1234567890";\n  int i;\n  i = strcspn (str,keys);\n  printf ("The first number in str is at position %d.\\n",i+1);\n  return 0;\n}\n</code></pre>\n'}, {'answer_id': 35494788, 'author': 'fnisi', 'author_id': 1884351, 'author_profile': 'https://Stackoverflow.com/users/1884351', 'pm_score': 0, 'selected': False, 'text': '<p>Write your own :)</p>\n\n<p>Code from a BSD licensed string processing library for C, called <a href="https://github.com/fnoyanisi/zString" rel="nofollow">zString</a></p>\n\n<p><a href="https://github.com/fnoyanisi/zString" rel="nofollow">https://github.com/fnoyanisi/zString</a></p>\n\n<pre><code>int zstring_search_chr(char *token,char s){\n    if (!token || s==\'\\0\')\n        return 0;\n\n    for (;*token; token++)\n        if (*token == s)\n            return 1;\n\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 48264846, 'author': 'snack0verflow', 'author_id': 9004745, 'author_profile': 'https://Stackoverflow.com/users/9004745', 'pm_score': 0, 'selected': False, 'text': '<p>You can write </p>\n\n<pre><code>s="bvbrburbhlkvp";\nint index=strstr(&amp;s,"h")-&amp;s;\n</code></pre>\n\n<p>to find the index of <code>\'h\'</code> in the given garble.</p>\n'}]
['https://Stackoverflow.com/questions/4824', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/75/']