0

I'm currently working on a program that will compare a set of input strings to a user input. The only condition is I'm not allowed to use built-in string operations; I must code them all from scratch.

The input strings is an array of strings like so:

char *input_strings[] = { 
    "Hello", "What is your name?", "How are you?", "Bye"
};

What I currently have is a bunch of functions:

1) uppercheck, which checks if a value is uppercase or not:

int uppercheck(int c){
    return (c >= 'A' && c <= 'Z');
}

2) lowercase, which converts a value to lowercase:

int lowercase(int c){
    if (uppercheck(c)){
        c + 0x20;
    }
    else{
        c;
    }
}

3) compstr, which compares two lowercase strings:

int result;
int compstr(char str1[], char str2[]){
    for(int i = 0; str1[i]; i++){
        if (lowercase(str1[i]) == lowercase(str2[i])){
            result = 1;
        }
        else{
            result = 0;
        }
    }
    return result;
}

The latter part of my code checks if the user string is equal to each entry in the input_strings array, like so:

char input[100]; // user input string
while (1) // infinite loop
{ 
    //Get the input string
    printf("> ");
    fgets(input, 100, stdin);

    // comparing strings:

    //3 base input conditions:
    if (compstr(input_strings[0], input) == 1)
    {
        printf("Hello\n");
    }
    else if (compstr(input_strings[1], input) == 1)
    {
        printf("My name is Name\n");
    }
    else if (compstr(input_strings[2], input) == 1)
    {
        printf("I am fine\n");
    }
    //exit condition:
    else if (compstr(input_strings[3], input) == 1) 
    {
        printf("Bye\n"); 
        break;
    }
    //invalid input:
    else 
    {   
        printf("I do not understand\n");
    }
}
return 0;

My problem is that the program will output "Hello" for ANY input, even when there's no input at all. I thought that the compare function would make sure the two strings are identical, but it doesn't seem to work and I'm not sure how to approach it from here. Sorry if I overdid the code but I figured I'd add as much info as possible. Any help would be heavily appreciated!

12
  • 2
    Your function (3) will return 1 for every two strings with the same first character Commented Mar 12, 2019 at 16:39
  • Instead of a multi-pass solution, you may want to do a single-pass where you have a lookup table to do lower-case conversion and then compare the remapped values from string A and B to see if they're equal. Move character by character, and stop if the lengths mismatch or the values mismatch. Commented Mar 12, 2019 at 17:03
  • I'd recommend making a function to get the length, and a function to lowercase to reduce the amount of repeated code and to make things clearer for yourself. Commented Mar 12, 2019 at 17:24
  • @EugeneSh. does the for loop not iterate over the entire string? Or does return stop the loop from fully iterating? I'm quite new to C so I'm trying to figure out these smaller things. Commented Mar 12, 2019 at 17:24
  • 1
    Are you allowed to use the character-type functions from <ctype.h>? They're not string functions, so you nominally should be able to do so. If so, use them: isupper and tolower() seem to be what you want — though you could speed things up by noting that tolower() only changes upper-case letters; anything else is passed through unchanged, so you don't need to test for upper-ness — just convert everything with tolower() and compare the results. Commented Mar 12, 2019 at 17:33

1 Answer 1

1

In conclusion, you forgot to write return in the lowercase function and you didn't put a break; after result = 0; and the code should work fine.

Explanation:

There is only one place in your code that prints Hello so we know that the expression printf("Hello\n") is being executed in every loop. This means that the if statement immediately above this expression is being evaluated to true every time, which can only happen if the function compstr() always returns a 1 regardless of what input is. Now, let's dive into the compstr() function and see where the 1 comes from.

Well, the variable result is set to 1 if lowercase(str1[i]) is equal to lowercase(str2[i]). Okay, so our problem is that lowercase(str1[i]) is always the same as lowercase(str2[i]). Check your lowercase() function. You did not write any return statement in the function at all, and so it will always return a garbage number regardless of what c is. Basically, all you have to do is change c + 0x20; to return c + 0x20; and c; to return c;. That's it. Very simple mistake.

Now, the much bigger problem is that you do not have a break statement after you assign result = 0; in the compstr() function like this:

...
        else{
            result = 0;
            break;
        }
...

Why is this break important? Well, it is only relevant when str1 is longer than str2. See, the terminating condition for your for loop involves str1 only, and so even when we reach the last character in str2, we still keep looping if str1 is longer than str2. Let's look at an example scenario. Let's run the program and type Hello. The program stores Hello in input like this input = {'H', 'e', 'l', 'l', 'o', '\0', ...}, and then checks with the first input condition with compstr() and prints Hello. Now type Bye. Now, the array input is {'B', 'y', 'e', '\0', 'o', '\0', ...}. Notice how the o from Hello, is still there? The for loop in compstr will set result to 0 on the first character comparison which is fine but without the break, it will keep on looping even after comparing the last e in Bye because you are comparing it with Hello which has 5 characters, not 3. On the last loop, your if statement will compare the 5th character of Hello and 5th character of Bye, which is the letter o for both. and so result is set to 1 in the end of the loop and so the compstr function returns 1 and then the program prints Hello instead of Bye, even though you typed Bye, because Bye is shorter than Hello. Try, How are you? and it will print I am fine normally, but now if you type Hello or Bye, you will still get I am fine, because input looks like this: 'H', 'e', 'l', 'l', 'o', '\0', 'e', ' ', 'y', 'o', 'u', '?', '\0' .... The e you? at the end is garbage value from a few steps back when we stored How are you? in this very same input variable.

Sign up to request clarification or add additional context in comments.

Comments

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.