لماذا أستمر في الحصول على الأخطاء والفشل


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

إذا قمت بتشغيل الكود الخاص بك:

ج
#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

وهو ما تطلبه المهمة – نوعًا ما.

لذا تحقق من كيفية استدعاء وظيفتك – فقد تكون هذه هي مشكلتك!
ثم اقرأ المهمة مرة أخرى: ماذا يجب أن تكون النتيجة إذا قمت باختبار “اسمي Mike Hyphenated-Surname – هل يمكنك قراءة هذا؟”؟

コメント

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