Questions tagged [strchr]

A C library function which searches for the first occurrence of a character in a string.

strchr is a C library function found in the header string.h. The declaration for it is

char *strchr(const char *str, int c)

This function searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str. It returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found.

86 questions
44
votes
5 answers

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

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.
bodacydo
  • 63,809
  • 83
  • 206
  • 303
18
votes
4 answers

How does strchr implementation work

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…
Marc
  • 5,284
  • 5
  • 26
  • 50
10
votes
8 answers

How to convert a char* pointer into a C++ string?

I have a C++ string. I need to pass this string to a function accepting a char* parameter (for example - strchr()). a) How do I get that pointer? b) Is there some function equivalent to strschr() that works for C++ strings?
Moeb
  • 9,731
  • 27
  • 79
  • 109
6
votes
1 answer

How come C standard library function `strchr` returns pointer to non-const, when given `const char *` as first argument?

Compilation of given code sample with gcc/g++ succeeds. There is no error for strchr call, which obviously assignes const char * to char *. I've found strchr is declared as char * strchr(const char *, int) on two different sources pubs.opengroup.org…
MKPS
  • 175
  • 7
4
votes
1 answer

PHP STRCHR equivalence to JavaScript

Is there a function in javascript that is equivalent to strchr of php? Please help me. thank you.
4
votes
4 answers

In C how do I find the '\' character in a string?

Suppose I have a string entered by the user asdfgh\hj, and I wish to find out the index of \ character in a String. How I can do it in C? I tried strchr() function as strchr("asdfgh\hj",'\') but compiler throws an error. Then I used == operator but…
ranaarjun
  • 245
  • 3
  • 5
  • 12
3
votes
5 answers

Are there any modern alternatives of std::strchr() for C++?

I am extensively using std::strchr() in my code, but recently i started thinking about making my code more readable and modern. I wish there was function similar to std::any_of/std::string::find_first_of which is taking single character instead of…
JoJo
  • 43
  • 3
3
votes
4 answers

Using strchr() to count occurrences of a character in a string

I'm almost finished with the class semester, and I'm working on an assignment to write a function to find the number of a certain character in a string, given the function prototype the teacher assigned. I know I must be doing something stupid, but…
3
votes
2 answers

STRCHR vs STRRCHR difference?

I would like to know the difference between the two different uses. I believe the difference in some what very subtle. This is an explanation taken from the IBM reference manual. However maybe my english is bad, I just can't visualize the…
hayonj
  • 1,339
  • 1
  • 19
  • 26
2
votes
1 answer

What if NULL is passed in strchr()

I have my code running in Linux. I see my program getting aborted when a NULL is passed to strchr() function. This doesn't seem to happen when running in AIX. Can anyone tell why this difference in behavior for strchr() ? Thanks
G Sij
  • 21
  • 1
  • 3
2
votes
1 answer

How do I tokenize a char array input into a char and a string?

I'm trying to ask the user for an input of say, 3 characters. I want to separate the first character and the last two from each other. So if "A13" is a user input, I want to store 'A' in a separate char and "13" in a separate char[].…
turing042
  • 97
  • 7
2
votes
1 answer

Preloading custom strchr() - ubuntu crashes

I implemented that strchr() global strchr strchr: cmp byte[rdi], 0 je end cmp [rdi], sil je end add rdi, 1 jmp strchr end: mov rax, rdi ret When I…
NanoPish
  • 935
  • 1
  • 12
  • 27
2
votes
1 answer

Advance pointer and get 2 more characters after strchr in C

I'm trying to get 2 more characters after finding the first occurrence with strchr over a char pointer. The string can look like: foo;bar;2012 -> should output foo;b foo;caz; -> should output foo;c foo; -> should output foo (there are…
Peter D
  • 47
  • 5
2
votes
2 answers

Efficient memcspn

Does anyone know of an efficient implementation of a memcspn function?? It should behave like strcspn but look for the span in a memory buffer and not in a null terminated string. The target compiler is visualC++ . Thanks, Luca
luca
  • 339
  • 1
  • 5
  • 7
2
votes
3 answers

Strchr removing the part I want

So I am fooling around with strchr to get part of a string from a file: void manipulateComputers(char *name) { name[strlen(name)-2] = '\0'; printf("%s\n", name); char *ptr = strchr(name, ' '); printf("%s\n", ptr); } At the first…
Bryan
  • 117
  • 2
  • 13
1
2 3 4 5 6