-9

The following is my code for an interesting problem:

Basically I want to see if a and b both end up at 0 by the end of the function based on an input. For example, if the input is "UD", it should return true, but if the input is "LL", it should return false.

bool judgeCircle(char* moves) {
    int a = 0;
    int b = 0;

    char[] apples = moves;

    for (int i = 0; i < strlen(moves); i++)
    {
        if (apples[i] == 'U') { a++;}
        if (apples[i] == 'D') { a--;}
        if (apples[i] == 'L') { b--;}
        if (apples[i] == 'R') { b++;}     
    }

    if (a && b == 0) {return true;}
    else {return false;}
}

Any help would be appreciated

ncoder
  • 1
  • 3
  • 9
    Have you heard of `strlen`? – cs95 Aug 17 '17 at 10:15
  • I tried doing the following strlen(apples), I then proceeded to access first letter A by writing if (apples[i] == "A"), However I still get an error – ncoder Aug 17 '17 at 10:18
  • `strlen` accepts `const char *` as an argument. *I tried doing the following `strlen(apples)`,* Then what did you get?? That should return 2. *I then proceeded to access first letter `A` by writing `if (apples[i] == "A")`, However I still get an error* What error did you get? By the way, `"A"` is a string in C. If you want a character, use single quotes: `'A'`. Please refer to your C manual. – lurker Aug 17 '17 at 10:19
  • duplicate is only for the first question. You shouldn't ask two questions in one. But your second is *very basic C* as well, just write `char x = apples[0];`. –  Aug 17 '17 at 10:21
  • @ncoder see my comment about string versus character. You're trying to compare characters to string pointers. – lurker Aug 17 '17 at 10:22
  • 1
    The code doesn't make sense. What is `moves`, where is it declared? –  Aug 17 '17 at 10:25
  • More over: what about using a debugger to trace through the program line by line inspecting the values of the relevant variables to learn what is *really* going on? – alk Aug 17 '17 at 10:27
  • @ncoder please see [mcve] ... you should add one and make sure it compiles when copy&pasted. –  Aug 17 '17 at 10:27
  • 2
    @ncoder you have many errors in the code that are all basic C knowledge errors. Just fixing one of them won't fix your code. And it doesn't make sense for us to debug your code for you one little bug at a time. You need to go back and study your basic C. All the changes suggested so far are correct. – lurker Aug 17 '17 at 10:28
  • If defining `char* apples = "AP";` then `apples` is a pointer pointing to *read-only* memory. If you want to modify its content make `apples` an array by doing `char[] apples = "AP";`. – alk Aug 17 '17 at 10:28
  • 1
    The code, called with `"AP"`, gives `true` as it is presented right now. Still unclear what exactly is the problem. –  Aug 17 '17 at 10:31
  • 1
    Chameleon question :( @ncoder we are not here to do incremental code repairs – Martin James Aug 17 '17 at 11:08
  • 1
    This question has changed too much to even make sense. I've voted to close on the basis that it's unclear what's being asked. It seems evident that the OP needs a basic C tutorial, not assistance with a single specific issue. – lurker Aug 17 '17 at 11:35

2 Answers2

3

This code doesn't even compile. The main problem is here:

char[] apples = moves;
  1. This is invalid syntax. If you want to declare an array, the [] goes after the identifier, like char apples[].

  2. Even when corrected, a pointer (moves) is not a valid initializer for an array:

    • When you leave out the dimension of the array in the declaration, the dimension must be deduced from the initializer. This is impossible with just a pointer, there's no size information in a pointer.
    • Assigning an array is not possible in C.

If you really want to copy a string to a char array, you'd have to do something like this:

char apples[strlen(moves) + 1]; // one extra byte for terminating 0
strcpy(apples, moves);

This uses a variable-length array, which was introduced in C99, is optional in C11, but supported by most compilers. For alternatives, please do some research yourself.


Additionally, this line doesn't do what you probably think it does:

    if (a && b == 0) {return true;}

The condition is evaluated as a && ( b == 0 ). If a is not 0, this is logically true, so the whole expression is true exactly when a is not 0 and b is 0. You seem to want:

    if (a == 0 && b == 0) {return true;}

What's really uhm ... "surprising" ... about the code is the fact that the array serves no purpose at all. You could just directly access the characters through moves without ever taking a copy:

bool judgeCircle(char* moves) {
    int a = 0;
    int b = 0;

    for (int i = 0; i < strlen(moves); i++)
    {
        if (moves[i] == 'U') { a++;}
        if (moves[i] == 'D') { a--;}
        if (moves[i] == 'L') { b--;}
        if (moves[i] == 'R') { b++;}     
    }

    if (a == 0 && b == 0) {return true;}
    else {return false;}
}

This doesn't change anything about the semantics of the function, but just leaves out the one part that you got completely wrong.

1

There is no need to use another array and also the way you have initialized apples[] array is wrong.

Even then if you want to use another array, then you can change the apples[] declaration as follows:

char apples[100];
strcpy(apples, moves);

or else:

char *apples = moves;

Hope it will help !!

amol13
  • 362
  • 4
  • 11