//This program deletes a character inputted by the user recursively
#include <iostream>
usingnamespace std;
char find (char *a, char key) //Function gets passed array (but makes it a pointer) and key
{
if (*a == '\0') //If first index is empty, whole array is empty
{
return 0;
}
elseif ((*a == key) || (*a == (key+32)) ) //If first index is our key
//Move on to next recursive call, ignore this
//Plus 32 because this takes care of upper and lower-case in ASCII
{
return find(a+1, key);
}
else //If first index is not our key
//Return the character and move to next recursive call
{
return (*a) + find (a+1, key);
}
}
int main ()
{
char a[50]; //Char array to hold individual letters of the string
char key; //Char variable to hold the key
int i = 0; //Int variable for index purposes
cout << "This program will delete a specified character!";
cout << "\nEnter a string, marking the end with '!' \n\n";
cout << "String: ";
cin >> a[i];
while (isalpha(a[i]) && a[i] != '!') //While we have a letter
//It is not the end marker
{
cin >> a[++i]; //Input into next index
}
cout << "\n\nKey (upper-case PLEASE): ";
cin >> key; //Get key from user
cout << endl << "New string: " << find (a, key);
return 0;
}
What you need to do when deleting 'characters' from a 'string'.
You must locate the specified character, then you must move all forwarding
characters one to the left. Then you must add a new NULL TERMINATED character ('\0') to the end of that new string, which in my case would be previous_size - 1.
1 2 3
if (*p == specified_character) /*move all characters from this point +1 to the left by one, add null terminated,
and then return the same memory address that was passed to the first call to recursive_call*/;
else recursive_call(++p,specified_character);
EDIT:
Should I be doing it this way where it returns the character one by one to the console?
In my opinion it would be more performance supportive if you didn't.
Because as opposed to returning the whole string 'once' and re-enter the recursive function only required times(which is the lenght of the string),
by returning a single character at a time, you would have to re-enter the function and leave the function twice the amount -1 than you would by returning the whole string. You would also have to re-enter the cout function that many times too.
//This program deletes a character inputted by the user recursively
#include <iostream>
usingnamespace std;
void remove (char *a)
{
while (*a != '\0')
{
*a = *(a+1);
a++;
}
*a = '\0';
}
char find (char *a, char key) //Function gets passed array (but makes it a pointer) and key
{
if (*a == '\0') //If first index is empty, whole array is empty
{
return 0;
}
elseif ((*a == key) || (*a == (key+32)) ) //If it is our key
//Call remove function to move everything one over to the left
//Plus 32 because this takes care of upper and lower-case in ASCII
// Call recursive function for next index
{
remove (a);
return find(a+1, key);
}
else //If it is not our key
//Move to next index
{
return find (a+1, key);
}
}
int main ()
{
char a[50]; //Char array to hold individual letters of the string
char key; //Char variable to hold the key
int i = 0; //Int variable for index purposes
cout << "This program will delete a specified character!";
cout << "\nEnter a string, marking the end with '!' \n\n";
cout << "String: ";
cin >> a[i];
while (isalpha(a[i]) && a[i] != '!') //While we have a letter
//It is not the end marker
{
cin >> a[++i]; //Input into next index
}
cout << "\n\nKey (upper-case PLEASE): ";
cin >> key; //Get key from user
cout << endl << "New string: " << find(a, key);
cout << a;
return 0;
}
It gets rid of the key that we input, but starts adding weird symbols at the end..
For example, if I type in Alabama as my string... and 'a' as my key..
The output is "lbm!@@" ... The first @ is actually another greek symbol.
From your output, I believe its not understanding the end of the string for some reason.
I think the major confusion is that you pass the pointer by value, and the value you point to keeps on changing in each recursive call.
Why not pass a simple iterator integer to the find function? That will make things much more easier to visualize. I will have to think about this a little bit more.
BTW, The find function returns a char, why not void?
Okay. I could not get the cin to work on my compiler. Or maybe I'm making some mistake. But I just hard coded in a c string, and implemented the recursive delete.But here is how the recursive delete function should work: