2

I've got some code in my program that essentially does this

char array[3] = {'g', 'w', 'a'};
char array2[3] = {'h', 'o', 'd'};
int i; 
int b = 1;
for (i = 0; i<=2; i++){
    if (array[i] != array2[i]){
        b = 0;
    }
}
if (b == 1){
    printf("true");
}
else{
    printf("false");
}    

}

When I run it it outputs true no matter what values are in the arrays, even if they are completely different. Why aren't they being compared properly?

Here's the actual code because people can't seem to reproduce my error.

#include <stdio.h>
#include <string.h>
int compare(char word1[], char word2[]){
    int i, k, same = 1, j, a, b;

    for (i =0; i<=79; i++){
        for (k=i; k <= 79; k++){
            if (word1[i] > word1[k]){
                char temp = word1[i];
                word1[i] = word1[k];
                word1[k] = temp;
            }
        }
    }


    for (a =0; a<=79; a++){
        for (b=a; b <= 79; b++){
            if (word2[a] > word2[b]){
                char temp = word2[a];
                word2[a] = word2[b];
                word2[b] = temp;
            }
        }
    }

    for (j =0; j <= 79; j++){
        putchar(word1[j]);
        putchar(word2[j]);
        putchar('\n');
        if (word1[j] != word2[j]){
            same = 0;
        }
    }
    if (same = 1){
        printf("Anagrams");
    }
    else{
        printf("Not Anagrams");
    }
}

int split(char array[]){
    int a, b, c, second = 0, count = 0;
    char word2[80] = "", word1[80] ="";
    for(a=0; a <= 79; a++){
        if (array[a] != 0){
            if (array[a] == ' '){
                second = 1;
                count = 0;
            }
            else{  //add to array
                if (second == 0){
                    word1[count] = array[a];
                    count ++;
                }
                else{
                    word2[count] = array[a];
                    count ++;
                }
            } 
        }
        else{
            break;
        }
    }
    compare(word1, word2);
    putchar('\n');
    return 0;
}
int main(){
    char temp, words[80] = "";
    int count = 0;
    while ((temp=getchar()) != EOF){
        if (temp == '\n'){
            split(words);
            memset(words, 0, 80);
            count = 0;
        }
        else{
            words[count] = temp;
            count ++;
        }
    }
    if (count > 0){
        split(words);
    }
    return 0;
}
11
  • 1
    Are you sure this is the code you actually ran? Commented Oct 9, 2015 at 4:05
  • 2
    The posted code looks OK to me. Please post an MCVE. Commented Oct 9, 2015 at 4:06
  • @TimBiegeleisen The code is fine until and unless he use arrays as string . Commented Oct 9, 2015 at 4:07
  • 1
    @Jay We need to see code that reproduces the problem otherwise there's no point asking. The code you've shown us looks right. Commented Oct 9, 2015 at 4:08
  • 1
    @Paul Nice hyoothesis...could be right Commented Oct 9, 2015 at 4:10

2 Answers 2

7

I suspect the problem is here:

if (same = 1){

That should have been

if (same == 1){

Using -Wall in gcc reveals the problem quickly.

cc -Wall -std=c99     soc.c   -o soc
soc.c: In function ‘compare’:
soc.c:35:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if (same = 1){
Sign up to request clarification or add additional context in comments.

3 Comments

Wow I am the dumbest guy ever. That solved it, thanks for the help.
@Jay, don't feel bad. You are not the first to make that mistake and certainly won't be the last.
@Jay: people say that C is a risky language that lets you make these kinds of errors, but no one who uses C seriously compiles without -Wall (and probably even -Werror), you will do well to use them, especially if you are learning.
3

logic error, replace if(same = 1) to if(same).

1 Comment

@immibis Correct, but a lot of matured 'c' programmers tend to use if(same) just because you don't care what value `same' holds, as long as its not 0...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.