Can you help me with these two problems with recursion?
1) I have a string : define the dimension
2) I have a string and a character: find the first position of the character in the string
Thanks to all!!
The first I made but it has't recursion....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C++:
#include <iostream>
usingnamespace std;
int main() {
char string[] = "Prova";
int conta = 0;
while (string[conta] != 0)
conta++;
cout<<"La stringa e' "<<string<<endl;
cout <<"La lunghezza della stringa e' "<<conta<<endl;
return 0;
}
I done so (i dont' know nullptr and also char*) but I have this messagge (""expected primary-expression before 'char') in the line "
cout << lunghezza(string)";
That code he posted already solves your second problem. Simply replace 0 in line 5's if statement with the value in the string you're looking for and the function will return it's first position.
^With the only tweaks being an extra parameter/argument for convenience, this code will print 3. Understand that an array starts at 0, so at element 3 is the letter 'v'.
The teacher didn't teach me "search"...
Is there another method without using an array so the position is real position (for example 'v' is the element 4)?
"search" is simply a variable name.. Look at line 11, I made the variable search = 'v' when it goes into the function lunghezza. And once inside the function in line 5, I can put the variable in as an argument since the function will have a value for "search" is by the time it's called.
You have to use your array if you're going to search through it. The whole point of this assignment is likely to help teach you recursion, which can be used to search through an array (though there are easier and better means).
And element 4 is NOT 'v', it would be 'a'. An array starts at 0. If you were to do this:
std::cout << string[4];
Your output would be 'a', because element 4 is 'a'. If you for some reason want the number of where 'v' visually is, you can simply add 1 to the output of the function:
The function can do both. With the slight tweak that I made you can find the element where your letter is. If instead of looking for a letter you look for 0 or NULL, you'll know how large your array is.
@zapshe,
You need to deal with the case where the character is not found or risk hanging your PC. See @MikeStgt's code for one possibility, although this problem is not a good one to do by recursion as you would have to propagate "not found" all the way back up the stack, testing for it all the time.
#include <iostream>
usingnamespace std;
int findPosition( char c, constchar *text )
{
if ( !*text ) return -1;
if ( *text == c ) return 0;
int ans = findPosition( c, text + 1 );
return ans < 0 ? -1 : ans + 1;
}
int main()
{
char text[] = "nil desperandum";
for ( char c : {'a','b','c','d','e'} )
{
int pos = findPosition( c, text );
if ( pos < 0 ) cout << c << " is not found\n";
else cout << c << " is first found at (zero-based) position " << pos << '\n';
}
}
I am searching a simple solution.. similar to this but without mistakes.....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int cerchiamo( constchar a, string b)
{
if ( b == a ) return 1;
if ( b == 0 ) return -1;
elsereturn 1 + cerchiamo(a, string + 1);
}
int main ()
{
string b;
cin>> string;
constchar a;
cin a;
return 0;
}
You need to deal with the case where the character is not found or risk hanging your PC. See @MikeStgt's code for one possibility, although this problem is not a good one to do by recursion as you would have to propagate "not found" all the way back up the stack, testing for it all the time.
Yes, I'm just trying to show him the concept first, it seems he's not understanding well.
mpg, you should likely try to practice and solidify your current C++ knowledge, I recommend going to learncpp.com .
Line 16 in your code a few posts ago doesn't have ">>". Line 4 in your recent code is checking if the ENTIRE string "str" is equal to "" - and what is ""? "" isn't anything valid.