-3

I am trying to figure out how to use toupper and tolower using pointers. I thought I was on the right track. I managed to get the pointers correct for the uppercase letters but for some reason, it won't work for the lowercase. Any advice would be helpful.

#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 */
Barmar
  • 741,623
  • 53
  • 500
  • 612
Drew
  • 23
  • 1
  • "/* ------------------ */" What is the purpose of comments like this?? –  Feb 27 '17 at 22:35
  • 3
    Please expand on "won't work". What is the input, expected output and actual output? – kaylum Feb 27 '17 at 22:36
  • 6
    You modify `s1_ptr` in your `toupper()` loop, and do not reset it for the `tolower()` loop. – EOF Feb 27 '17 at 22:37
  • You are supposed to type in any letters and they appear in capital letters and in lowercase letters. The inputted capital letters are working as they are outputted but the lowercase displays absolutely nothing. – Drew Feb 27 '17 at 22:38
  • Reset `s1_ptr` before 2nd while-loop. E.g `printf ("\nThe line of text in lowercase is:\n"); s1_ptr = text;`. Also `putchar(tolower(*s1_ptr++));` is redundant. Change it to `putchar(*s1_ptr++);` – BLUEPIXY Feb 27 '17 at 22:52

1 Answers1

1

Thats because it never goes into the second loop because you modified your pointer. Before the second loop reset your pointer to s1_ptr = text then it will work.