C程序中的Bool错误检查字符大小写

时间:2015-03-10 02:51:37

标签: c

您好我正在编写一个C程序,并且我有一个名为bool is_proper_case(char str [])的辅助函数,如果大写字母仅出现在str的开头或紧跟在句点之后,则返回true。而且,只有大写字母或NULL终止符可以跟随一段时间。例如:

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);

已编辑的代码:

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;
      }

我的问题是:我无法断言上述陈述,我没有断言。(有一些逻辑错误)

我不知道我在这个片段中缺少什么,请知道,我是C编程的初学者,请耐心等待。

提前致谢

2 个答案:

答案 0 :(得分:1)

一次,if (str[i]=='.'&& (str[i]<='z' && str[i+1]>='a'))对于ASCII始终为false,因为点不在z和a之间。

此外,只有当str [i]是大写字母时才会到达此行,因此它似乎根本不在正确的位置。

答案 1 :(得分:1)

以下代码应该可以实现您的目标。我写了一堆评论来帮助你理解代码中发生了什么。希望你能得到这个想法。你可能想要支持一些额外的东西:

  1. 一段时间后的单一空白区域。像这样。
  2. 连续三个时期。它继续......
  3. 所有上层“单词”,例如美国。

  4. #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;
    }