Charlie M
Charlie M

Reputation: 273

C++ recursion scanning with strings

I have an assignment I am working on that uses recursion. I'm still having a little trouble understanding recursion, or at least how it all works, but I think I'm starting to grasp it, even though I'm not all that sure why anything works.

My assignment has two parts, but for the moment, I just need a little help with the first part. Here's what I have to do:

Write a recursive function that will return the position of the first occurence of a >character within a C String

This is what I have so far...

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int test(string s, char x);

int main ()
{
  test("lets test for the letter s", "s" );
}

int test(string s, char x)
{
if(s.length() == 0)
    return 0;
else if (s[0] == x)
    return 1 + test(s.substr(1, s.length()), x);
else
    return test(s.substr(1, s.length()), x);
}

So i think this should work, but I'm a little confused as to how to get the function to test anything. I'm pretty sure I have the string part done correctly in my function call in main, but I can't get the char to accept a value. The way I understand it, i should enter the text I want to scan, and then the character I want to look for. Can anyone tell me what I am doing wrong, or even I'm even close with the recursive function?

Upvotes: 0

Views: 302

Answers (3)

taocp
taocp

Reputation: 23644

You should do something like the following:

int main ()
{
   test("lets test for the letter s", 's'); 
                                   //should pass char constant 
                                 //not string literal for second parameter
}

int test(string s, char x)
{
   if(s.length() == 0)
      return 0;
   else if (s[0] == x)
      return 1 + test(s.substr(1, s.length()-1), x); 
                                //^^^second parameter of substring is length
   else
      return test(s.substr(1, s.length()), x);
}

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 361730

Character constants go in single quotes. To test your function write something like:

cout << test("lets test for the letter s", 's') << endl;

As for your recursive function, you're close. The if statements have the right tests, you just need to adjust the return statements a bit.

if (s.length() == 0)
    return -1;

If the string is empty the character isn't found. I suggest returning -1 rather than 0 because a return value of 0 suggests (to me) that the character was found at position 0. -1 is the traditional return code from functions like these when a character isn't found.

else if (s[0] == x)
    return 0;

Do you see why this is return 0? You found the character x at index 0, so that's what you should return: 0!

else
    return 1 + test(s.substr(1, s.length() - 1), x);

The last test is the only one that needs to be recursive. Here's where you put the 1 +. And you also need to reduce length() by 1.

Upvotes: 1

user995502
user995502

Reputation:

"s" will be treated as a char array or a string. To represent a single char you should use 's'

int main ()
{
  cout << "location  = " << test("lets test for the letter s", 's' );
                                                               ^^^^
}

Upvotes: 0

Related Questions