gpt4 book ai didi

c - C程序中用于检查字符大小写的 bool 错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:45 24 4
gpt4 key购买 nike

你好,我正在编写一个 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 编程的初学者所以请耐心等待。

提前致谢

最佳答案

下面的代码应该可以完成你想要的。我确实写了一堆注释来帮助您理解代码中发生的事情。希望你能明白。您可能想要支持一些额外的东西:

  1. 句点后的单个空白。像这样。
  2. 连续三个时期。它继续......
  3. 全上“词”,例如 USA。

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

关于c - C程序中用于检查字符大小写的 bool 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28955354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com