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
0
votes
3 answers

find a char array in another char array in c

#include using namespace std; int main () { char name[10]; cin>>name; char str[] = "Thomas"; char * pch; pch=strchr(name,str); if (pch!=NULL) { cout<<"Foud"<
Wizard
  • 10,985
  • 38
  • 91
  • 165
0
votes
1 answer

Can't fix heap-buffer-overflow error on my C code

I need help fixing an fsanitize=address error on this code. If I compile my .c program with the flags "fsanitize=address -g" I get the following error: ==93042==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000107903a7c at pc…
0
votes
1 answer

Returning Unique Character Array (Unique String) from User Input

So, my main task is to return a character array consisting of unique characters, but in the same order as they are within the user input. I have worked on a function below that seems feasible, but returns nothing whenever I type anything in as…
ArbIn
  • 43
  • 5
0
votes
2 answers

return ((char *)&s[i]); What & means in this statement?

I'm new to these wanderings, sorry if it's a stupid question. but can they enlighten me what what's doing there? char *ft_strchr(char *s, int c) { int i; i = 0; if (!s) return (0); if (c == '\0') return ((char…
galhog
  • 11
  • 2
0
votes
2 answers

What does the: = '-'; or ='/' means in strchr(I know it does locate work and strrchr of last occurence)?

What does the = '-'; or = '/' mean in strchr (I know it does locate work and strrchr of last occurrence)? This code does create two files. #include #include int main() { FILE *fpA = fopen("output_A.txt", "w"); FILE…
Eronsee
  • 13
  • 1
0
votes
0 answers

Trying to sort for the nth word in an array

I am working on a code that takes parameters: -d (which sorts text descending) -L (which sorts lines per length) -L and -d (which does both) -n (which sorts the text to the nth word) I'm trying to read a file and sort its output like in the…
0
votes
1 answer

Find absolute path in linux using strchr

How can we use the function strchr to find absolute path in linux, for example: Input: /home/test/sample Output: /home/test I try to write something like this: int main() { char* string = "/home/test/sample"; char* pos; pos = strchr(string,…
Pedro Gómez
  • 214
  • 1
  • 8
0
votes
2 answers

How to connect a string with the appropriate integer after sorting?

Can someone help me with this problem in C? I need to sort leaderboard in alphabetical order and that works, and by scores. I use atoi() for converting string to integers, but names stay in the order they were left. Here is the function for…
0
votes
1 answer

Can't figure out how strchr works in C

I have this piece of code that is suppose to split a string depending on the delimiter. It seems to work when the delimiter is '\0' (null character) but when I try to make it work with '\n' (newline) I end up with a weird result that I can't…
0
votes
1 answer

How to resolve 'munmap_chunk(): invalid pointer Aborted' when using strchr

I am writing a function in C using strchr. Basically, given a string from parameter, the code will identify any '\n' exists in (char content[]) and copy the string before '\n' to str using strncpy. String after '\n' is copied using strchr. The…
Calmen Chia
  • 325
  • 1
  • 8
0
votes
2 answers

How can i strack multiple parts of a string using strchr() in C?

Im new to C, and I would like to learn how can I Strack multiple words from a string just using strchr().. cant use strtok, scanf or similar functions.. I have string: char myImput[51]="my flight to New Orleans, is at 12:30" the string format is: …
JeanBook
  • 31
  • 7
0
votes
2 answers

strchr(), APT_String and subtraction operation

I'm working with a program written in C that involves comparing hypehenated surnames. For example, it might compare Mary Jay-Blige to Mary Kay-Blige. The code that finds the hyphen and sets a variable to it's position is: APT_String LAST_NAME char *…
user898763452
  • 169
  • 1
  • 13
0
votes
1 answer

C strchr works with NULL value on HPUX but segfaults on RHEL

I'm moving some code from HPUX 11.11 to RHEL 7.5 and it includes a function that uses strchr. On HPUX it runs fine, and on RHEL there is a segmentation fault. I isolated the code and created the following simple test, with the subsequent results. …
Kyle
  • 97
  • 5
0
votes
1 answer

Using strchr() in dev c++, invalid conversion from 'char' to 'int'[-fpermissive]

i want the character founded by what i'm input to to find it so here's the code, but it's always error in "hasil=strchr(str,karakter);". it's says "invalid conversion from 'char' to 'int'[-fpermissive]" #include #include…
0
votes
1 answer

Random Bytes In C Output

I just wrote my first program in C and it is a cesarean shift implementation. It works as expected with short inputs, but sometimes produces seemingly random bytes at the and of the output and I cannot figure out why. I have tried looking at the…
Michael Hoefler
  • 105
  • 2
  • 9