Tại sao tôi liên tục gặp lỗi và thất bại

lập trình


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

Những gì tôi đã thử:

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

Giải pháp 1

Nếu tôi chạy mã của bạn:

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

Sau đó tôi nhận được những gì tôi mong đợi:

Kết quả
hellO worlD

Đó là những gì bài tập yêu cầu – đại loại vậy.

Vì vậy, hãy kiểm tra xem bạn đang gọi hàm của mình như thế nào – đó có thể là vấn đề của bạn!
Sau đó đọc lại bài tập: Kết quả sẽ ra sao nếu tôi kiểm tra “Tên tôi là Mike có gạch nối-Họ – bạn có đọc được cái này không?”?

コメント

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