6
int count(string s){
    if(s == "")
      return 0;
    if(s.length == 1)
      return 1;
    return 1 + count() //This is what I can't figure out. How to traverse the string.
    //I just need a hint, not a full on answer.
}

I dont know how to traverse a string.

MikroDel
  • 6,705
  • 7
  • 39
  • 74
corn_flakes
  • 85
  • 1
  • 8
  • 2
    Hint: The size of a string is 1 + the size of the string with the first (or last) character removed. The size of an empty string is zero. – zackg Dec 11 '12 at 08:42
  • Sounds like homework to me.. now what you probably want to do on the second last line: return 1 + count(s.substr(0, s.length() - 1)); – Nils Dec 11 '12 at 08:44
  • 1
    Yeah it's homework. Nothing special but it's bugging me pretty good. – corn_flakes Dec 11 '12 at 08:46
  • 4
    are you sure then that you need to use `std::string` vs `char*`? `if(s.length == 1)` looks almost like cheating :) – Karthik T Dec 11 '12 at 08:47
  • You use string's `length` method which already returns size. What's the point of this exercise? –  Dec 11 '12 at 08:48
  • That's what i thought, why use recursive to count a string length when there's already a built in function to do that. The prof wants to familiarize us with recursion in all its glory. The good and bad i guess... – corn_flakes Dec 11 '12 at 09:00

6 Answers6

10

Hint: use substr() in your recursion.

Also, you have two base cases. One of them has three issues:

  1. it has a syntax error in it;
  2. it relies on being able to compute the length of the string (which is what your function is supposed to do);
  3. it is unnecessary given that you have the other base case.
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

I don't think you example makes any sense, you use length which already returns length in your calculation. If I were your tutor, I wouldn't have accepted this as a valid solution.

You probably need to use const char*

int count(const char* s){
    if(*s == '\0')
      return 0;
    return 1 + count(s + 1);
}
  • 1
    You don't _need_ `const char*`, not even `char*`. – John Dvorak Dec 11 '12 at 08:53
  • 1
    The problem is not that the string knows its length, but that the OP uses it as a base case. – irrelephant Dec 11 '12 at 08:53
  • From other examples i found they all used chars but the requirement for the question was to use a string data type. I know its silly to count the length in recursion when all you have to due is use length() function but i think the prof wants us to at least experience string recursion. – corn_flakes Dec 11 '12 at 08:57
  • @irrelephant, what is the point of calculating length of the `std::string` if it already knows it? I guess his assignment sounds like `calculate string size recursively` where `string` is meant to be c-string, not `std::string` –  Dec 11 '12 at 08:58
  • 1
    @user1893303, did he explicitly say `std::string` or just "string data type"? The latter probably means that you need `const char*` –  Dec 11 '12 at 09:00
1

If your purpose is to traverse a string, I suggest using an iterator (see std::string::begin).

template<typename It>
int count(It const begin, It const end)
{
  return (begin != end ? count(begin + 1, end) + 1 : 0);
}

int count(std::string const& s)
{
  return count(s.begin(), s.end());
}
Julien Royer
  • 1,419
  • 1
  • 14
  • 27
1

Maybe you would want to use substr.

0

I know you want a C++ solution, but still. Sometimes C is better than C++.

int count(const char *s)
{
  if(*s == 0)
    return 0;
  else return 1 + count(++s);
};

Call as count(str.c_str()).

Manoj R
  • 3,197
  • 1
  • 21
  • 36
0
  #include<stdio.h>
  main(){
  char str1[100];
  gets(str1);
  int i=0;i=len(str1,i);printf(" \nlength of string is %d",i);
  }
  int len(char s1[],int i) {
  printf("\n%c",s1[i]);
  int sum=0,count =1; 
  if(s1[i] == '\0') return 0;
  else
  return (count += len(s1,++i));
  }
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
Vikas Reddy
  • 45
  • 1
  • 2
  • 8