strchr() in C

Let me explain, what I did when I wanted to find out the first occurrence of a character and print the successive characters until I find the '\0'.



Output:
1
2
3
4
Passed String [Device::78:90:AB:67:12:34] len[25] chr[:] 
chrpos[::78:90:AB:67:12:34]
chrpos[p�1��$���$����b�p�
��$��]



strchr() in C

What is the problem in the above program ?? 

I have just returned a pointer to a local variable which is allocated on the stack. Once we exit from the function the variable chrpos is ceased to exist. 

If we want to return a string, then allocate it on heap, return it, use it and free it once you are done. - This also legit serves as the answer for why we need malloc.

#include <stdio.h>
#include<string.h>

#define SIZE 30

char* find_fist_occurrence_string(char *str, char chr);

int main()
{
    char str[SIZE] = "Device::78:90:AB:67:12:34";
    char chr = ':';
    char *first_occur = NULL;

    first_occur = find_fist_occurrence_string(str, chr);
    printf("chrpos[%s]\n", first_occur);
    return 0;
}

char* find_fist_occurrence_string(char *str, char chr)
{
    char *chrpos = NULL;
    int i = 0, j = 0, start = 0;

    chrpos = malloc(strlen(str));
    printf("Passed String [%s] len[%d] chr[%c] \n", str, strlen(str), chr);
    while (str[i] != '\0') {
        //printf("i[%d] str[%c]\n", i, str[i]);
        if ((start == 0) && (chr == str[i])) {
            start = 1; 
        }
        if (start == 1) {
            chrpos[j++] = str[i];
        }
        i++;
    }
    chrpos[j] = '\0';
    printf("chrpos[%s]\n", chrpos);
    return chrpos;
}

Output:
1
2
3
Passed String [Device::78:90:AB:67:12:34] len[25] chr[:] 
chrpos[::78:90:AB:67:12:34]
chrpos[::78:90:AB:67:12:34]

Now, the above program would be the answer to "Write your own strchr() in C"

But, doesn't it look more verbose, so let's find out if there is any predefined library function?

It is a C library string function which is used to find the first occurrence of the character in the passed string.

#include <stdio.h>
#include<string.h>

int main()
{
    //char str1[] = "Bangaloregklimogyiong";
    //char str1[] = "Bang\0";
    //char str1[] = "Ban";
    //char str1[] = "Bang\0jkl";
    //char str1[] = "Bangjkl";
    //char str1[] = "Banggjkl";
    //char str1[] = "Device:141.89.90.123:78:90:AB:67:12:34";
    char str1[] = "Device::78:90:AB:67:12:34";
    //char chr = 'g';
    char chr = ':';
    char *chrpos;

    int count = 1;
 
    printf("Given string %s\n", str1);
    chrpos = strchr(str1, chr);
    if (chrpos) {
        printf("After %d delmiter %s\n", count, chrpos);
        if (!strcmp((chrpos + 1), "\0")) {
            // char str1[] = "Bang\0";
            //char str1[] = "Bang\0jkl";
            printf("End of string \n");
            return;
       }
       printf("chrpos + 1 %s\n", chrpos + 1);
       chrpos = strchr((chrpos + 1), chr);
       if (chrpos) {
           count++;
           // char str1[] = "Banggjkl";
           printf("After %d delmiter %s\n", count, chrpos);
        } else {
           // char str1[] = "Bangjkl";
           printf("No delmiter found after %d \n", count);
        }
    } else {
        // char str1[] = "Ban";
        printf("No delmiter Found \n");
    }
    return 0;
}

Output:
1
2
3
4
Given string Device::78:90:AB:67:12:34
After 1 delmiter ::78:90:AB:67:12:34
chrpos + 1 :78:90:AB:67:12:34
After 2 delmiter :78:90:AB:67:12:34

Additional Observations

#include <string.h>                     
char *strchr(const char *s, int c);

chrpos = strchr(string, chr);

string   "128.132.132.130\0"
chr       '\'
chrpos  \0



If the character is found, it returns a pointer to the first occurrence of the character c in the string S. If it is not found, strchr() returns NULL.

It can accept the string as const char and single character, and It returns the string starting from the passed character.

string - 128.132.132.130\0
ptr - \0

We can also find out the first occurrence of '\n' as well. This will be useful to get each line from a file.

Eg: chrpos = strchr(string, '\n');

We can also find the first occurrence of space.

Eg: chrpos = strchr(string, ' ');

strtok will not get the successive characters after the passed character to be found, so we go for stchr().

If you want to get the last occurrence of the character c in the string, please click here.

References - Stackoverflow &  https://www.2braces.com/c-programming/c-strchr

Comments