Deleting a character recursively?

Help, I'm having trouble deleting a character inputted by the user recursively..

1- Should I be doing it this way where it returns the character one by one to the console?

2- Is there a way actually rebuild the string to "delete" these occurances of the key?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//This program deletes a character inputted by the user recursively
#include <iostream>
using namespace 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;
	}
	else if ((*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.
Last edited on
Tried fixing it, still having trouble...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//This program deletes a character inputted by the user recursively
#include <iostream>
using namespace 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;
	}
	else if ((*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?
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>

void remove(char* const a,int it)
{
 while(*(a+it)!='\0')
 *(a+it)=*(a+(++it));	
 return;
}
void find(char* const a,const char key,int it)
{
	if (*(a+it)=='\0')
	return;
	else 
	{
		if (*(a+it)==key)
		remove(a,it);
		find(a,key,++it);
	}
}	 

int main()
{
	char a[]="alabama!";
	char key='a';
	
	find(a,key,0);
	std::cout<<"New String:"<<std::endl;
	std::cout<<a<<std::endl;
	
	return 0;
}
Last edited on
I worked on this for about another 2 hours and got it to work.. My code looks somewhat similar, same ideas... Thanks for your help though !!!
Topic archived. No new replies allowed.