0

I would like to take a string input, convert them into either lowercase or uppercase and print them. So far, I have successfuly implemented that. Now, I want to modify my program such that if the input has whitespace in it, it will print all of the converted letter plus the whitespace

e.g.,

Enter a string: STACK OVERFLOW
Output: stack overflow

So here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<string.h>

#define MAX_CHAR 50
char to_lowercase(char letter);
char to_uppercase(char letter);

int main(void)
{
    char user_input[MAX_CHAR];

    printf("Enter a string: ");
    scanf("%s", &user_input);

    for (int i = 0; i <= MAX_CHAR; i++)
    {
        if (user_input[i] == 0){exit(EXIT_SUCCESS);}
        else{printf("%c", to_lowercase(user_input[i]));}
    }

    return EXIT_SUCCESS;
}

char to_lowercase(char letter)
{
    if ((97 <= letter) && (letter <= 122)) {return letter;} 
    else if ((65 <= letter) && (letter <=90)) {return letter + 32;}
    else if (letter == 32){return letter;}
    else
    {
        printf("Not a valid character!");
        exit(EXIT_FAILURE);
    }
}

char to_uppercase(char letter)
{
    if ((65 <= letter) && (letter <= 90)) {return letter;}
    else if((97 <= letter) && (letter <= 122)) {return letter - 32;}
    else if (letter == 32){return letter;}
    else
    {
        printf("Not a valid character!");
        exit(EXIT_FAILURE);
    }
}

My current approach is to check using the ASCII code for whitespace (#32), however this doesn't really work because when i type "STACK OVERFLOW", the program will only print "s"

5
  • the program will only print "s" Dones't it print "stack"? It prints "stack" for me, not only "s". Commented Apr 8, 2024 at 14:30
  • The %s specifier for scanf will read a space delimited word. If you want to read a whole line use fgets. Commented Apr 8, 2024 at 14:30
  • 2
    On a different note, please don't use magic numbers in your code. If the value 65 is supposed to be the ASCII encoded value for the character 'A' use the actual letter instead. Also note that ASCII is not mandated by the C specification, there are other encodings available still in use. That's why you really should be using the standard toupper and tolower functions instead of making your own. Commented Apr 8, 2024 at 14:33
  • Oh and the loop for (int i = 0; i <= MAX_CHAR; i++) can go out of bounds of the array. Change the condition to i < MAX_CHAR. But that will only protect you from going out of bounds, a better condition is user_input[i] != '\0'. That will end the loop when you get to the string terminator, and you don't need the terminator check inside the loop itself. Commented Apr 8, 2024 at 14:35
  • thank you guys. i'm new to programming in general, your help was really helpful Commented Apr 8, 2024 at 14:44

2 Answers 2

2

In scanf, %s only scans a string without spaces. It stops at the first white-space character. Change to use fgets(user_input, sizeof user_input, stdin);.

Also, do not use numbers for characters. Use character constants: ' ' for space, 'A' for the letter A, and so on. The compiler will automatically use the correct values for your C implementation, and the code is easier to read.

Be aware the C standard does not require the codes for letters to be consecutive. The code for N does not necessarily lie between the value for A and the value for Z. A program that needs to classify characters should include <ctype.h> and use isupper, islower, and so on, as in if (isupper((unsigned char) x)) return tolower((unsigned char) x);.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, so now i can get the whitespace. the only problem now is my program always prints "Not a valid character!"
@user23569449: There is a newline character at the end of the input.
0

Many issues.

  1. If you reach the end of the string do not exit the program. Same if the character is not a letter.
  2. Use fgets
  3. Do not use magic numbers.
  4. scanf is not good at reading lines as it stips on whitespace.
  5. use toupper - tolower functions from ctypes.h
#define MAX_CHAR 50
char to_lowercase(char letter);
char to_uppercase(char letter);

int main(void)
{
    char user_input[MAX_CHAR];

    printf("Enter a string: ");
    if(fgets(user_input, MAX_CHAR, stdin))
    {
        for (size_t i = 0; user_input[i]; i++)
        {
            printf("%c", to_lowercase(user_input[i]));
        }
    }

}

char to_lowercase(char letter)
{
    if(letter >= 'A' && letter <= 'Z') letter += 'a' - 'A';
    return letter;
}

char to_uppercase(char letter)
{
    if(letter >= 'a' && letter <= 'z') letter += 'A' - 'a';
    return letter;
}

https://godbolt.org/z/fxdG58G6G

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.