Program stops mid-way.

I'm doing an assignment for a C++ class, and I can't seem to figure out why my program keeps hiccoughing in the middle of running. The program runs fine when I run only the strlen_recursive function, but when i run the strcpy_recursive function, it stops and gives me an error. Any help would be greatly appreciated!
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
#include <string>
#include <iostream>
using namespace std;

int strlen_recursive(char* in);
char* strcpy_recursive(char*in, char* copy);
int main()
{
	int length(0);
	char string[50];
	char copy[500];
	cin>>string;
	length=strlen_recursive(string);
	cout<< length<< endl;
	strcpy_recursive(string, copy);
	cout<< copy;
	return 0;
}

int strlen_recursive(char* s)
{
	
	if(*s=='\0')
		return 0;
	else
		return (1+strlen_recursive(s+1));
}

char* strcpy_recursive(char* in, char* copy)
{
	if((*in=*copy) != '\0')
		strcpy_recursive(in+1,copy+1);
	return in;
}
closed account (Dy7SLyTq)
try *in = *copy without the != '\0'
That still doesn't work. I'm not sure what the problem is.
closed account (Dy7SLyTq)
oh wait i think i know the problem. so i believe that when u have an empty array of chars (in this case copy), copy[0] is \0 so its already at the end. and string currently points to the end because of strlen_recursive. so the first time they are called in and copy both equal '\0'
How do I fix that?
Can anyone help with this?
closed account (Dy7SLyTq)
sorry i cant think of a solution
Look at the order and use of your parameters. You pass (string, copy) when you call it, and the function copies copy into in.
Topic archived. No new replies allowed.