¿Por qué sigo recibiendo errores y fallas?

programación


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

Lo que he probado:

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

Solución 1

Si ejecuto tu código:

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

Entonces obtengo lo que espero:

Resultado
hellO worlD

Que es lo que pide la tarea, más o menos.

Así que verifique cómo llama a su función; ¡ese puede ser su problema!
Luego lea la tarea nuevamente: ¿Cuál debería ser el resultado si pruebo “Mi nombre es Mike con guion-apellido – ¿puedes leer esto?”?

コメント

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