2

i wanted to write a code which would allow me to find a position of a fist occurence of a letter, this is what i have come up so far. As you can see, what the function returns is actually a value and not the index. Is there a way to find it without simply giving the initial value of index as in the code no.2?

char *recFirstPosition(char * source, int letter)
{

     if(*source == letter)
        return *source;

     if (*source =='\0')
      return 0;

     recFirstPosition(++source, letter);
}


char *recFirstPosition(char * source, int letter, int index)
{

     if(*(source+index) == letter)
        return index;

     if (*(source+index) =='\0')
      return 0;

     recFirstPosition(source, letter, ++index);
}
15
  • You need index to make this work unfortunately, or some sort of counting mechanism. Commented Dec 2, 2016 at 20:46
  • 1
    Neither of the functions returns a value for all control paths. Commented Dec 2, 2016 at 20:47
  • 1
    First step is to decide whether you want the function to return a char, a pointer, or an index (int)... Commented Dec 2, 2016 at 20:52
  • 1
    "I wanted to write a code but it does not return what I want". This is not your code is it? As commented above, if you want an index position returned the function type cannot be char*. Commented Dec 2, 2016 at 20:53
  • 1
    You want return source+index; Commented Dec 2, 2016 at 21:07

2 Answers 2

1

Simply detach * from the first return and add return for the recursive call of your first version.

char *recFirstPosition(char * source, int letter)
{

     if(*source == letter)
        return source;

     if (*source =='\0')
      return 0;

     return recFirstPosition(++source, letter);
}

It will make the code work. Your first version causes a type error.

The following is more readable version than the above:

char *recFirstPosition(char *source, char letter)
{
    if (*source == '\0')
        return NULL;
    else if (*source == letter)
        return source;
    else
        return recFirstPosition(++source, letter);
}

The above code also changed the type of the second parameter, but is written mostly inspired by several comments (Special thanks to Yuli and Dmitri).

And you may use the function as follows:

int main()
{
    char *s = "Hello";
    char *p = recFirstPosition(s, 'l');
    if (p != NULL) {
        int index = p - s;
        printf("%s[%d] = %c\n", s, index, *p);
    }  
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

While testing, it prints some number but i dont think that this number is the right posistion :/
This is the answer. She could also return NULL instead of 0 just to make more semantic sense (although 0 does the job)
@AnnaSobolewska use printf("%s") on the return value of this function and you'll see that it prints from letter on
Oh yes, it actually prints the string from the letter, but still, i need to print an exact position of this letter, right? so i would need a integer value as a result right? not a whole string? :/
So, what does this code return when the first character in source isn't letter or '\0'?
|
0

Here is what I think could work. Please test it more since I did not have enough time to work with it Edit: This return the position of the last occurrence but should give you enough to work with.
Edit2: Updated the function so now it works for

#include <stdio.h>

char *recFirstPosition(const char *s, int c, char *find){
    if(s==NULL) return NULL;
    if(*s == '\0') return (c == '\0') ? (char*)s : find;
    if(*s == c) return (char*) s;
    return recFirstPosition(s + 1, c, *s == c ? (char*)s : find);
}


int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=recFirstPosition(str,'s', NULL);
  // Uncomment the while loop to get all instances positions  
  //while (pch!=NULL)
  //{
    printf ("found at %d\n",pch-str+1);
  //  pch=recFirstPosition(pch+1,'s',NULL);

  //}
  return 0;
}

output Looking for the 's' character in "This is a sample string"... found at 4

2 Comments

just tested again, looks like the function is returning the position of the last occurrence. Since it's an assignment, I will leave that to you to figure out.
hint: you just need to modify the recFirstPosition to get only the position of the first occurrence ... also note, I am adding 1 to the position so it starts with 1 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.