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.

99 questions
54
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
  • 75,521
  • 93
  • 229
  • 319
21
votes
5 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
  • 6,051
  • 5
  • 26
  • 56
12
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
  • 10,527
  • 31
  • 84
  • 110
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
  • 8
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.
user2852776
  • 63
  • 1
  • 5
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
  • 1
  • 4
3
votes
2 answers

How can I read accented characters in C++ and use them with isalnum?

I am programming in French and, because of that, I need to use accented characters. I can output them by using #include and setlocale(LC_ALL, ""), but there seems to be a problem when I read accented characters. Here is simple example I…
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,429
  • 3
  • 21
  • 28
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

Getting incompatible integer to pointer conversion error in program. Unsure how/why exactly this is occurring but looking for an explanation

I'm trying to count how many dashes "-" there are in char p[]. I loop over the string, and use the strcmp function to compare what is at the p[i] location to "-". The strcmp function returns 0 if the they are the same. int howmanyDash( char p[] ){ …
zackychan97
  • 112
  • 1
  • 9
2
votes
2 answers

Shortening a function that counts the number of unique chars in a 2D array

I have a function that counts the number of unique characters in a 2D array by looping over it and increasing the count in each cell of a 1D array by 1 each time a character from the valid char array is found. I then loop over the 1D array and each…
Davospike
  • 91
  • 1
  • 9
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
  • 99
  • 10
2
votes
1 answer

Replacing one character with two in C

I want to replace a character by two characters in my string. void strqclean(const char *buffer) { char *p = strchr(buffer,'?'); if (p != NULL) *p = '\n'; } int main(){ char **quest; quest = malloc(10 * (sizeof(char*))); …
1
2 3 4 5 6 7