Write a function named FirstOcurence that takes as parameters an array of char(c-strings) and a character c. The function should return a pointer that point to the first occurrence of c in the string. If c is not found the function should return NULL.
The function prototype is: char* FirstOccrence(char arr[], char c)
You got a little confused here: if (p[i]==c). p is not an array, its a pointer to char. Of course your compiler won't complain about this but this won't work as you expect. Do your checking like: if (*p==c). Or you could just omit the temporary char pointer and do it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
char * FirstOccurence(char arr[], char c)
{
//call strlen once
int size=strlen(arr);
for(int i=0;i<size;i++)
{
if (arr[i]==c)
return arr+i;
}
return 0;
}
Enough is enough - Any thread like this (along the lines of - here is my problem do it for me, or here is my broken code fix it) should just be deleted - that is my opinion.
But (s)he had almost solved his/her problem by himself/herself. I'd say that the only thing (s)he did wrong was the choice of the name of the topic... All I did was correct a minor problem and provide a slightly more optimized solution...
It's not "illegal" by any means, but the fact that you're asking someone to solve your problems for you is pathetic. You're never going to learn anything if you keep having others do your homework for you.
Okay. To put all of this in far nicer terms, most of us regulars on this forum will never solve your problems for you. Don't even ask, as guestgulkan will probably shout at you again (not without reason; it gets tiring seeing so many posts of this type). You may ask what's wrong with your code, but never ask anyone to do anything for you. Not here.