top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Find the first occurrence of a character in a string using C?

0 votes
91 views

Write the prototype function in C: char * my_strchr (char * arr, char c);

The function should return the cursor to the first occurrence of the forwarded character in the character sequence or NULL if the forwarded character was not found.

i came up with solution also but it has to be string :

char *my_strchr(char *arr,char c)
{
int i=0;
char *p;
while(1)
{
if(i[arr] == c){p = i[arr];return p;break;}
if(i[arr] == '\0'){p= NULL;return p;break;}
i++;
}}

posted Apr 23, 2017 by Leon Martinović

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Just traverse the array and compare the each element with the target character, rest is simple.
Following is the sample code...

char * my_strchr(char * arr, char c)
{
    int i, len;

    len = strlen(arr);

    for(i=0; i<len; i++)
    {
        if(arr[i] == c)
        {
            return &arr[i];
        }
    }

    return null;
} 
answer Apr 23, 2017 by Salil Agrawal
Similar Questions
0 votes

Find the first non repeating character in a string.

For example
Input string: "abcdeabc"
Output: "e"

0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

+1 vote

How to find first unrepeated character in string using C/C++?

Input       Output     
abc         a    
aabcd       b
aabddbc     c
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...