KLMM
KLMM

Reputation: 93

Bool error in C program to check character case

Hello I am writing a C program, and I have a helper function called bool is_proper_case(char str[]) which returns true if uppercase letters only occur at the beginning of str or immediately after a period. Moreover, only uppercase letters or a NULL terminator can follow a period. Examples are:

assert(is_proper_case("Hello world.") == true);
assert(is_proper_case("hello world.") == true);
assert(is_proper_case("i like C language.very much.") == false);
assert(is_proper_case("This sentence is not.correct.") == false);

Edited Code:

bool is_proper_case(char str[]) {

int len = 0;

for (int j=0 ;str[j]!= 0 ; j++)
{

 len++;
}

int output = 0;

for (int i=0 ; i<=len-1 ; i++)

{
   if (str[i]=='.'&& (str[i+1]<='z' && str[i+1]>='a'))
   {
     output = 0;
   }

   else if ('A'<=str[i]<='Z')
   {
    output = 1;
   }

   else output = 0 ;
     }
       printf ("%d", output);
      return output;
      }

MY problem is: I am unable to assert the above statements, I am failing the assertions.(there is some logical error)

I don't know what I am missing in this snippet please hep, I am a beginner to C programming so please be patient.

thanks in advance

Upvotes: 0

Views: 115

Answers (2)

jweyrich
jweyrich

Reputation: 32260

The following code should accomplish what you want. I did write a bunch of comments to help you understand what's going on in the code. Hopefully you'll get the idea. You may want to support a few extra things:

  1. Single white-space after period. Like this.
  2. Three consecutive periods. It goes on...
  3. All-upper "words", like USA, for example.

#include <assert.h> // for assert()
#include <stdbool.h> // bool support in C99 or higher.
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h> // for strlen()

bool has_proper_case(const char *str) {
    // Start by assuming the string has proper case. Conforming.
    bool result = true;
    // How long is the string?
    const size_t len = strlen(str);

    // State variables
    bool is_upper = false;
    bool is_period = false;
    bool is_after_period = false;

    // Iterate over the entire string.
    for (size_t i=0; i < len; i++) {
        // This will only be true if the previous character was a period.
        is_after_period = is_period;
        // This will only be true if the current character is a period.
        is_period = str[i] == '.';
        // This will only be true if the current character is uppercase.
        is_upper = str[i] >= 'A' && str[i] <= 'Z';

        if (i > 0) // Are we past the beginning of the string?
        {
            // Uppercase is mandated after a period.
            if (is_after_period && !is_upper) {
                result = false; // Set the result to false. Non-conforming.
                break; // Break out of the loop.
            }
            // Uppercase is not tolerated anywhere besides after a period.
            if (is_upper && !is_after_period) {
                result = false; // Set the result to false. Non-conforming.
                break; // Break out of the loop.
            }
        } else { // Are we at the beginning of the string?
            // Uncomment if uppercase is mandated at the beginning of the string.
            // if (!is_upper) {
            //  result = false;
            //  break;
            // }
        }
    }

    return result;
}

int main()
{
    assert(has_proper_case("Hello world.") == true);
    assert(has_proper_case("hello world.") == true);
    assert(has_proper_case("Uppercase is mandated after a period.Test") == true);
    assert(has_proper_case("Uppercase is mandated after a period.test") == false);
    assert(has_proper_case("Uppercase is mandated after a period. test") == false);
    assert(has_proper_case("Uppercase is NOT tolerated anywhere besides after a period.Test") == false);
    return EXIT_SUCCESS;
}

Upvotes: 1

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16026

For once, if (str[i]=='.'&& (str[i]<='z' && str[i+1]>='a')) is always false for ASCII because the dot is not between z and a.

Furthermore, this line is only reached if str[i] is an uppercase letter, so it seems not in the right place at all.

Upvotes: 1

Related Questions