Mengapa saya terus mendapatkan kesalahan dan kegagalan

pemrograman

[ad_1]

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

Apa yang saya coba:

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

Solusi 1

Jika saya menjalankan kode Anda:

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

Lalu saya mendapatkan apa yang saya harapkan:

Hasil
hellO worlD

Itulah yang diminta oleh tugas tersebut – semacam itu.

Jadi periksa bagaimana Anda memanggil fungsi Anda – itu mungkin masalah Anda!
Kemudian baca kembali tugasnya: Apa hasilnya jika saya menguji “Nama saya Mike dengan tanda penghubung-Nama Belakang – bisakah Anda membaca ini?”?

[ad_2]

コメント

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