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
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
  • 1,379
  • 1
  • 19
  • 35
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
  • 369
  • 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
  • 3
  • 13
2
votes
0 answers

NASM strange behavior with al

I tried to run the following program: C code : int main() { char *s1 = "hello"; printf("string : %s\n", strchr(s1, 'l')); } assembly code: global strchr section .text strchr: push rbp mov rbp, rsp strchr_loop: …
2
votes
1 answer

Minimally invasive change for strchr on unsigned char * in C++ from a C codebase?

I'm trying to compile a C codebase as C++, tweaking some of the includes. It uses strchr() on unsigned char pointers, e.g.: #include #include // Cannot modify this file with any non-C-isms, but let's say an include can // be…
2
votes
1 answer

C function strchr - How to calculate the position of the character?

I have this example code for the strchr function in C. /* strchr example */ #include #include int main () { char str[] = "This is a sample string"; char * pch; printf ("Looking for the 's' character in \"%s\"...\n",str); …
eljobso
  • 352
  • 2
  • 6
  • 20
1
vote
2 answers

Why is there an error that the pointer is always null or true depending on the if statement wording?

I'm not very versed in C so working with pointer errors is not going well for me. Any help would be appreciated. I have two versions of this piece of code and no matter what I've changed with the initializing or the if statement, there is a logic…
Sarah Cohen
  • 706
  • 1
  • 7
  • 11
1
vote
2 answers

Why my returned value of strchr is ignored?

I have to make a function, that will code my sentence like this: I want to code all words with an o, so for example I love ice cream becomes I **** ice cream. But my function ignores the result of strchr. And I don't know why. This is my…
chr
  • 25
  • 4
1
vote
5 answers

Make own strchr() function but case-insensitive

My exercise requires that I can use case-insensitive input. My approch is that I use the tolower and toupper function. How can I convert the array to lowercase letters? void KULstrcichr(char *arr, char search) { printf("Return value when…
1
vote
4 answers

c language strchr segmentation fault character replace

I need to replace every occurence of '&' to ',' in a C string. I did this and it works Code 1: char *val, *querydup; . . . val=strchr(querydup,'&'); while(val != NULL) { *val=','; val=strchr(querydup,'&'); } In order to be "elegant" I tried the…
user905292
  • 21
  • 3
1
vote
4 answers

Return a pointer at a specific position - Assembly

I am a beginner in Assembly and i have a simple question. This is my code : BITS 64 ; 64−bit mode global strchr ; Export 'strchr' SECTION .text ; Code section strchr: mov rcx, -1 .loop: inc rcx …
Vroxy
  • 79
  • 7
1
vote
2 answers

Can't understand the usage of strchr to get the position of a charcter in a string in C

This code will output the index of a charcter in a string : #include #include int main(void){ char str[100]="birds are dying"; char *p; p = strchr(str,'e'); int i=p-str; printf("%d" , i); return 0; } the…
1
vote
1 answer

strchr function keep giving me core dumped error

I am trying to write code that copy and print the word in text file on the screen with getline() and strchr() functions. so this is my code: void read_teams(char* text) { FILE *fp=fopen(text,"r"); char* tname=NULL; size_t tname_size=0; …
1
vote
2 answers

How do I count how many times a letter appears in a string? C programming

I am trying to write a program to tell me how many 'e' letters are in the phrase "Yesterday, all my troubles seemed so far away". I am trying to do this using strcmp function and I want to compare each letter in the phrase to the letter 'e' and then…
adamlsmith981
  • 63
  • 1
  • 8