为什么我不断收到错误和失败


Description
Write a function that takes one string and, capitalize the last character
of each word in uppercase and the rest in lowercase.

A word is a section of string delimited by spaces/tabs or the start/end of the
string. If a word has a single letter, it must be capitalized.

A letter is a character in the set [a-zA-Z]

I keep getting errors and failures, which I cannot figure out

我尝试过的:

#include <stdio.h>
#include <ctype.h>

char* rcapitalize(char* str) {
    int i = 0;
    while (str[i] != '\0') {
        if (isalpha(str[i])) {
            str[i] = tolower(str[i]);
            if (!isalpha(str[i + 1])) {
                str[i] = toupper(str[i]);
            }
        }
        i++;
    }
    return str;
}

解决方案1

如果我运行你的代码:

C
#include <stdio.h>
#include <ctype.h>

char* rcapitalize(char* str) 
    {
    int i = 0;
    while (str[i] != '\0') 
        {
        if (isalpha(str[i])) 
            {
            str[i] = tolower(str[i]);
            if (!isalpha(str[i + 1])) 
                {
                str[i] = toupper(str[i]);
                }
            }
        i++;
        }
    return str;
    }
int main()
    {
    char data[] = "Hello World";
    printf("%s\n", rcapitalize(data));
    return 0;
    }

然后我得到了我所期望的:

结果
hellO worlD

这就是作业所要求的——某种程度上。

所以检查一下你是如何调用你的函数的——这可能是你的问题!
然后再次阅读作业:如果我测试“我的名字是迈克连字符-姓氏 – 你能读懂这个吗?”,结果应该是什么?

コメント

タイトルとURLをコピーしました