Recusive strcpy

This is a discussion on Recusive strcpy within the C++ Programming forums, part of the General Programming Boards category; Need help in Recusion Code: char* StrCpy(char* output, char* input) { if (*input != NULL) { *output = *input; *output++; ...

  1. #1
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18

    Recusive strcpy

    Need help in Recusion
    Code:
    char* StrCpy(char* output, char* input)
    {
    	
    	if (*input != NULL)
    	{
    		*output = *input;
    		*output++;
    		*input++;
    
    		StrCpy(output,input);
    	}
    	else *output= NULL;
    	return output;
    }
    If i Give input as "ASD"
    output is "SD"
    where is the problem..
    need quick help
    keep cool but dont freeze
    compilers:
    MS VC++ 6.0
    Unix g++ compiler

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Perhaps because you return the value of output AFTER you have incremented it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    It works for me. How are you defining your actual parameters input and output in the program that calls it?

    Here's mine:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char* StrCpy(char* output, char* input);
    
      char outstr[100];
      //char instr[100] = "This is a test"; // try this : still works
      char instr[100] = "ASD";
    
      cout << "instr  = <" << instr << ">" << endl;
      StrCpy(outstr, instr);
      cout << "outstr = <" << outstr << ">" << endl;
    
      cout << endl << "Now, you enter a word to copy: ";
      cin  >> instr;
    
      cout << "instr  = <" << instr << ">" << endl;
      StrCpy(outstr, instr);
      cout << "outstr = <" << outstr << ">" << endl;
    
      return 0;
    }
    
    
    char* StrCpy(char* output, char* input)
    {
        
        if (*input != NULL)
        {
            *output = *input;
            *output++;
            *input++;
    
            StrCpy(output,input);
        }
        else *output= NULL;
        return output;
    }
    Tested on Windows XP with Borland bcc, Microsoft Visual C++, and GNU g++ (Cygwin).

    Tested on Linux with GNU g++.



    Dave

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It works for me.
    You must not have been paying attention to the output. I got the same problem as the OP.

    >where is the problem..
    You have several. The first is that NULL and '\0' may not evaluate to the same thing. In C++ they probably will, but since NULL is used in pointer context and not character context, you should avoid using it as you were. Next, to be strictly correct, input should be made const because you never change the contents of the string. And lastly, your actual problem. It is as Salem said. If you refrain from using ++ in your recursive functions in favor of adding 1 to the recursive call, you can avoid problems like these. Here are my changes:
    Code:
    char* StrCpy(char* output, const char* input)
    {
      if (*input != '\0')
      {
        *output = *input;
        StrCpy(output + 1,input + 1);
      }
      else
        *output = '\0';
    
      return output;
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    With all due respect to your expertise, I repeat that "it works for me". I submitted a complete program using his test string, and ran the thing on the compilers that I indicated. My point was, I was not trying to get him to improve his style just yet; I was trying to understand what his problem was so that he could fix it.

    (If it ain't broke, you can't fix it.)

    Will you post a complete program that breaks with ++ but works the other way? I would like to run it through my compilers. I am very interested in finding things that work in one programming environment that break in others, and understanding the differences.

    Note a confusion factor is in his routine where he has
    Code:
    *input++;
    *output++;
    It would undoubtedly make more sense to have
    Code:
    input++;
    output++
    These have the same effect (incrementing local copies of the variables input and output).

    I didn't change this in my response to him, since it had nothing to do with causing a problem.

    As for your points:

    Yes, I know that NULL is a pointer type, and, in fact g++ gave me the warning with his original code. I changed it to '\0' to get rid of the warning. This had nothing to do with his problem, so I changed it back to duplicate his code exactly before running the test with different input strings.

    As far as making the input string const: that's OK, and probably good programming practice, but had nothing to do with his problem.

    At a given level, calling a function with a value of (input+1) is no different than incrementing the variable (input), then calling with this incremented value.

    Dave
    Last edited by Dave Evans; 03-06-2004 at 12:06 PM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    I rather suspect the difference the OP sees is down to

    StrCpy(outstr, instr);
    cout << "outstr = <" << outstr << ">" << endl;

    Vs.

    cout << "outstr = <" << StrCpy(outstr, instr) << ">" << endl;

    The former will work whether you mess up the increment or not, but the latter will show the off-by-one error.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Ahhh, now I get it. This is kind of important. That's why I asked for an example.

    When somebody tells me something doesn't work, I need to know how he tested it.

    I completely missed the point that the return value of the function was wrong. I was paying attention to he actual copying (which worked OK), but there was a real bug in the function, which has now been fixed.

    On the other hand, when I submit a test program and someone tell me, "You must not have been paying attention to the output," it's not very helpful. My program gave the expected output, but wasn't an adequate test of the function in question.

    (You can't prove a program is correct by testing.)

    Thanks a lot.

    Dave
    Last edited by Dave Evans; 03-06-2004 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 08:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 11:34 AM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 10:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21