-3

ポインターを使用して toupper と tolower を使用する方法を理解しようとしています。私は正しい軌道に乗っていると思いました。大文字のポインターを正しく取得することができましたが、何らかの理由で小文字では機能しません。どんなアドバイスも役に立ちます。

#include <stdio.h>
#include <ctype.h>
void safer_gets (char array[], int max_chars);

main()
{

    /* Declare variables */
    /* ----------------- */
    char  text[51];
    char *s1_ptr = text;
    int   i;

    /* Prompt user for line of text */
    /* ---------------------------- */
    printf ("\nEnter a line of text (up to 50 characters):\n");
    safer_gets(text ,50);


    /* Convert and output the text in uppercase characters. */
    /* ---------------------------------------------------- */
    printf ("\nThe line of text in uppercase is:\n");
    while (*s1_ptr != '\0') 
        {
            *s1_ptr = toupper(*s1_ptr);          
            putchar(toupper(*s1_ptr++));
        }

    /* Convert and output the text in lowercase characters. */
    /* ---------------------------------------------------- */
    printf ("\n\nThe line of text in lowercase is:\n");
    while (*s1_ptr != '\0') 
        {
            *s1_ptr = tolower(*s1_ptr);          
            putchar(tolower(*s1_ptr++));
        }

    /* Add carriage return and pause output */
    /* ------------------------------------ */
    printf("\n");
    getchar();
} /* end main */

/* Function safer_gets */
/* ------------------- */
void safer_gets (char array[], int max_chars)
{
    /* Declare variables. */
    /* ------------------ */
    int i;

    for (i = 0; i < max_chars; i++)
        {
            array[i] = getchar();

            /* If "this" character is the carriage return, exit loop */
            /* ----------------------------------------------------- */
            if (array[i] == '\n')
                break;
        } /* end for */

    if (i == max_chars )
        if (array[i] != '\n')
            while (getchar() != '\n');
    array[i] = '\0';
} /* end safer_gets */
4

1 に答える 1