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
1
vote
3 answers

Confusion on how to use strchr in C

char *strchr( const char *s, int c ); I understand that strchr locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned. So why does below code outputs num to…
Leon
  • 346
  • 3
  • 15
1
vote
1 answer

Why does the strchr not take the program into an if condition?

I am trying to run the following code, but during execution, the code does not go into the if condition. Why does the code not enter the if condition during runtime? I have marked the problem condition. Running this program on Windows 10. Thread…
lynx
  • 23
  • 4
1
vote
1 answer

find = strchr(st, '\n'); replaced with *find = '\0';

I read a snippet of code from C Primer Plus, and tried hard to understand *find = '\0'; #include #include char *s_gets(char *st, int n); struct book { char title[40]; char author[40]; float value; } int main(void)…
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
1
vote
1 answer

strchr() finding '\n' where there seemingly is none

I'm using a modified fgets() function called s_gets() that removes a newline from input or discards any of the remaining characters in the input buffer. It looks like the following; char *s_gets(char *str, int n, FILE *pf) { char *ret_val; …
BlaqICE
  • 309
  • 2
  • 11
1
vote
1 answer

why strchr returns not a NULL when the second argument is a string into a single quotation marks ('text')

I tried to write implementation of the strchr(). char *ft_strchr(const char *s, int c) { (unsigned char)c; while (*s) { if (*s == c) return ((char *)s); s++; } if (c == '\0') return ((char…
ivoriik
  • 155
  • 1
  • 11
1
vote
1 answer

Can Strchr function find numerous letters in a string in c++?

Diff FBullAndCow::EDifficulty(std::string diff) const { if ((diff.length() > 1)) { return Diff::Not_Number; } else if (!strchr(diff.c_str(), '3' || '4' || '5' || '6' || '7' || '8')) { return Diff::Not_Number; …
CorpseDead
  • 113
  • 2
  • 7
1
vote
3 answers

How to write your own strchr in c using pointers?

Ok, so I have an assignment here from my professor. Here it is: Write a function called strchr406. It is passed 2 parameters: a string and a char Here is the prototype for the function: char *strchr406(char str[], char ch); The function should…
nastypluto
  • 119
  • 4
  • 13
1
vote
2 answers

how to get substring in c with strchr

I'm trying to fetch part of a string. I have the following code: #include #include #include char mystring[]="The quick brown fox jumps over the lazy dog"; char word1[]="The"; char * posb,pose; char * word2; int…
spf
  • 11
  • 1
  • 3
1
vote
2 answers

Using strchr() to create multiple strings from one master string

I'm working on a program in C for a class that requires me to take a request-line and break it down into its subsequent pieces. It's for learning purposes, so I can expect a fairly standard request-line. Thinking about the problem, I was going to…
Caleb Jay
  • 2,159
  • 3
  • 32
  • 66
1
vote
3 answers

Subtracting char array from char pointer produces an int?

In the strchr reference at http://www.cplusplus.com/reference/cstring/strchr/, this example is provided. /* strchr example */ #include #include int main () { char str[] = "This is a sample string"; char * pch; printf…
freesushi
  • 69
  • 7
1
vote
3 answers

strchr causing segmentation fault in C

I am having problem with a C program. I know that strchr() is causing the problem, and it is returning Segmentation Fault My code is as follows: char *pointer; pointer = strchr(string, character); I don't know why I'm getting the error…
Jordan
  • 327
  • 8
  • 23
1
vote
1 answer

reading in a variable number of long ints on one line using strchr and strtol

My function is not quite done yet, but what I'm going to do is: To read in a string of numbers separated by spaces. Extract the numbers from that string. Convert them to long ints. Return the number of numbers read in. int input( int a, int b, long…
Frey1337
  • 413
  • 1
  • 4
  • 8
0
votes
1 answer

Find first occurrence of unescaped character

What is the best way to find the first unescaped occurrence of a character in a given string? This is how I did it, but I have a feeling it's overly complicated. /* * Just like strchr, but find first -unescaped- occurrence of c in s. */ char…
Robert Kajic
  • 8,689
  • 4
  • 44
  • 43
0
votes
2 answers

strchr not working with char[]

I am working on ROT13 for c++ practice. however this bit of code here returns an error and fails to compile, i do not understand why! I am posting a snippet of code in the following lines string encode(string &x) { char alphabet[] =…
Alter Ego
  • 39
  • 3
  • 13
0
votes
3 answers

Need to know about c++ strchr function and how it works

I want to know about strchr function in C++. For example: realm=strchr(name,'@'); What is the meaning for this line?
Srini
  • 173
  • 1
  • 2
  • 4