-4

This is the question I'm working on : http://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/

Here's my code in Java for one pass :

/*If a character isn't repeating, copy it to str[j].
 * Find start and end indices of repeating characters. Recursively  call again
*  And starting position now should be end+1. Pass j and starting    position  */
public class removeDuplicates {

public static void main(String[] args) 
{


    char[] str = {'c','c'};
    removeDups(str,0,0,0);
    System.out.println(str);

}

public static void removeDups(char[] str,int j, int start,int flag) 
{
    /*Check if start character is repeating or not. If yes , then loop till you find
     * another character. Pass that characters index(new start) into a recursive call*/

    if(start == str.length-1) 
    {
        if(flag!=1)
        {
            str[j] = str[start];
            j++;

        }
        if(j<=str.length-1) 
        {


            str[j] = '0';

        }

    }

    while(start<str.length-1 && str[start]!='0') 
    {

        if(str[start+1]!=str[start]) 
        {

            str[j] = str[start];

            start++;
            j++;
            if(start==str.length-1) {removeDups(str,j,start,flag);}
        }


        else 
        {
            char ref = str[start];

            while(str[start]==ref) 
            {

                if(start<str.length-1)
                {
                    start++;
                }
                else 
                {
                    flag =1;
                    break;

                }

            }

            removeDups(str,j,start,flag);
            return;

        }

    }




}
}

This works as expected. Here I'm just trying to use a 0 instead of \0 character as in C. Now when I translate the code to C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

 void removeDups(char *str,int j, int start,int flag) 
{
    /*Check if start character is repeating or not. If yes , then loop till you find
     * another character. Pass that characters index(new start) into a recursive call*/



    if(start == strlen(str)-1) 
    {
        if(flag!=1)
        {
            str[j] = str[start];
            j++;

        }
        if(j<=strlen(str)-1) 
        {


            str[j] = '\0';



        }

    }

    while(start<strlen(str)-1 && str[start]!='0') 
    {

        if(str[start+1]!=str[start]) 
        {

            str[j] = str[start];

            start++;
            j++;
            if(start==strlen(str)-1) {removeDups(str,j,start,flag);}
        }


        else 
        {
            char ref = str[start];

            while(str[start]==ref) 
            {

                if(start<strlen(str)-1)
                {
                    start++;
                }
                else 
                {
                    flag =1;
                    break;

                }

            }

            removeDups(str,j,start,flag);
            return;

        }

    }





}

int main() 
{


char str[] = "abcddcba";

int len =
while()

for(int i=0;str[i]!='\0';i++) 
{

    printf("%c",str[i]);
}
printf("\n");
}

The above code gives different results as compared to the Java code.Its virtually identical , just that I'm using strlen() instead of str.length(as in Java).

The interesting part is : if I change the portion to

if(j<=strlen(str)-1) 
        {


            str[j] = '\0';

            return;
}

it works perfectly. I've just added a return statement to the if statement.

Why is this happening ? Identical code producing different results in C and Java

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Hormigas
  • 1,429
  • 5
  • 24
  • 45

2 Answers2

1

You are using return statement and subsequently all code below that return is being excluded from running for that iteration.

Also, You may want to understand what is \0 is and how it's different than 0. Here's link:

What does the \0 symbol mean in a C string?

Community
  • 1
  • 1
akhileshmoghe
  • 603
  • 8
  • 21
0

In C, assigning a character in a string to '\0' changes the length, so strlen() will return a different result after that. In your Java code, you're using an array, and an array length never changes. You're setting the character to '0' instead of '\0', which are two different things, but even if you did set it to '\0', it still wouldn't change the length. I haven't examined your entire code, but this is one obvious thing that would cause different results.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Yes, that's the intended behavior. – Hormigas Dec 18 '16 at 07:27
  • You're saying the intent of the Java code was to change the length? In that case, the Java code doesn't work. `str.length` doesn't change just because you set an element to `'0'`. As I said, however, I haven't studied your code thoroughly enough to see what works and what doesn't work. – ajb Dec 18 '16 at 07:33