My code will print error message twice and still run

Viewed 129

I am having an issue where my code should look for a duplicate value and throw an error if true otherwise set a bool value to "true". But for some reason it is printing the error message twice and still executing the rest of the code. I even tried another option but both gave the same result (the commented out if statement)Please help me out here:

bool key_check(string argv[])
{

int string_c = strlen(argv[1]);
bool key_c;
int dup[string_c];

if (string_c == 26)
{

    for (int i = 0; i < string_c; i++)
    {
        if(isalpha(argv[1][i]))
        {
                if(strcmp(argv[1], argv[1]) != 0)
                {
                    key_c= true;
                }
                else
                {   //this line is being printed twice and instead of terminating code it allows it to run
                    printf("Key must not contain duplicates.\n");
                    return 1;
                  
                }

                /*if(dup[argv[1][i] - 65] == false && dup[argv[1][i] - 97] == false )
                {
                    key_c= true;
                }
                else
                {
                  printf("Key must not contain duplicates. %c \n", argv[1][i]);
                  key_c = false;
                  return 1;
                }*/
            //}
        }
        else
        {
            printf("Key must only contain characters. \n");
            key_c= false;
            return 1;

        }
    }
}
else
{
   printf("Key must contain 26 characters. \n");
    key_c= false;
}

return key_c;
}
1 Answers

Hy, I guess you mean returning false when you talk about throwing an error.

In C everything except 0 evaluates to true so if you say:

return 1; // true

in the context of your function then you announce success to the calling function.

Therefore this makes little sence:

 printf("Key must only contain characters. \n");
 key_c= false;
 return 1;

Here you set key_c to false but then return true.

Sample Solution

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

#define TRUE 1
#define FALSE 0

int
key_check(char *argv)
{
    const int EXPECTED_LEN = 26;
    char buffer[26] = {0};
    
    if(strlen(argv) != EXPECTED_LEN) {
        printf("Key must contain 26 characters. \n");
        return FALSE;
    }

    for(int i = 0; i < EXPECTED_LEN; i++) { 
        for(int j = 0; j < i; j++) {
            if(argv[i] == buffer[j]) {
                printf("Key must not contain duplicates.\n");
                return FALSE;
            }
        }

        buffer[i] = argv[i];
    }
    

    return TRUE;
}

int main() {
    char *s1 = "abcdefghijklmnopqrstuvwxyz"; 
    char *s2 = "abcdefghijklmnopqistuvwxyz"; 
    char *s3 = "abcdefghijklmnopqistuvwxy";

    printf("%s has only unique characters.. %s\n", s1, key_check(s1) ? "Yes" : "Nope");
    printf("%s has only unique characters.. %s\n", s2, key_check(s2) ? "Yes" : "Nope");
    printf("%s has the right length.. %s\n", s3, key_check(s3) ? "Yes" : "Nope");
}

I use a char buffer like you to store all letters that have already occurred. Then I check if the string length matches the expected length of the key and return false if not. Otherwise, the function continues and iterates over the input string. I use a nested for loop to check if a character has already occurred and continue the search if not, adding the current character to the buffer. If the string only consists of unique letters then we reach the end of the function and return 1 to indicate success.

Note:

This nested for loop is inefficient for larger strings.

Related