I am struggling with an assignment for my first c++ class. I am hoping someone can help me in the right direction. I need to write a "recursive version of strlen in c strings." According to my handout, my function should look like this, "int str_length(char s[])".
My main problem is trying to get the user to enter a string or cstring of an undetermined length and use it for the function call. I would really appreciate all the help and direction I can get.
I have played around with my code so much now that I am rather lost. It would seem I would fix one issue and create a new one. I think I have my function written correctly but here is the code if there is a better/correct way of doing it.
#include <iostream>
#include <cstring> //included both until I find my solution
#include <string>
using namespace std;
//string or char sentence; This part of my struggle
char choice = 'Y';
int str_length(char s[]);
int main()
{
while ((choice != 'n') && (choice != 'N'))
{
cout << "Enter a sentence. ";
//user entry cin, getline etc
cout << sentence;
//cout << str_length(sentence);
cout << endl;
cout << "Do you want to have another run? Y/N ";
cin >> choice;
}
}
int str_length(char s[])
{
// if we reach at the end of the string
if (s == '\0')
{
return 0;
}
else
{
return 1 + str_length(s + 1);
}
}