C Program to Convert a String to Lowercase Without strlwr()

C Program to Convert a String to Lowercase Without strlwr()

Converting a string to lowercase is a very common operation in C programming. Normally, you might use the strlwr() function, but like strupr(), it is not part of the standard C library and may not work in every compiler. That is why it is useful to know how to implement this operation manually.

Understanding lowercase conversion also gives you insight into how characters are stored in memory using ASCII values. In the ASCII table, uppercase letters 'A' to 'Z' have values from 65 to 90, while lowercase letters 'a' to 'z' range from 97 to 122. The difference between them is 32. This means that by adding 32 to an uppercase letter, you can convert it to lowercase.

In this article, we will write different programs to convert a string to lowercase without using strlwr(). We will cover methods using loops, pointers, and recursion. Each approach will help you better understand how strings and character encoding work in C.

Understanding the Problem

The goal is to take a string and change every uppercase letter into its lowercase form. For example:

  • "HELLO WORLD""hello world"
  • "C PROGRAMMING""c programming"
  • "Mixed Case""mixed case"

All non-alphabetic characters like numbers, punctuation, and spaces should remain the same.

Program 1: Using a Loop

The simplest way is to go through the string character by character using a loop. If the character is between 'A' and 'Z', add 32 to convert it to lowercase.

#include <stdio.h>

int main() {

    char str[100];
    int i;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (i = 0; str[i] != '\0'; i++) {

        if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }

    }

    printf("Lowercase string: %s", str);

    return 0;

}

This method is easy to understand and works well for beginners.

Program 2: Using Pointers

Instead of indexing the array, we can use a pointer to move through the string. This shows another way of accessing and modifying characters in memory.

#include <stdio.h>

int main() {

    char str[100];
    char *p;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (p = str; *p != '\0'; p++) {

        if (*p >= 'A' && *p <= 'Z') {
            *p = *p + 32;
        }

    }

    printf("Lowercase string: %s", str);

    return 0;

}

This approach is efficient and makes good use of C’s pointer arithmetic.

Program 3: Using Recursion

Recursion can also be used to process one character at a time. It makes the code look shorter but requires careful handling of the base case.

#include <stdio.h>

void toLowercase(char *s) {

    if (*s == '\0') return;

    if (*s >= 'A' && *s <= 'Z') {
        *s = *s + 32;
    }

    toLowercase(s + 1);

}

int main() {

    char str[100];

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    toLowercase(str);

    printf("Lowercase string: %s", str);

    return 0;

}

This recursive approach works the same way as looping, but it processes characters one by one by calling the function repeatedly.

Program 4: Using tolower() from <ctype.h>

The standard library function tolower() automatically converts uppercase letters to lowercase. This simplifies the code.

#include <stdio.h>
#include <ctype.h>

int main() {

    char str[100];

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower((unsigned char)str[i]);
    }

    printf("Lowercase string: %s", str);

    return 0;

}

tolower() only affects uppercase letters and leaves other characters unchanged.

Program 5: Using a Function for Modularity

We can wrap the lowercase conversion logic in a function for reusability.

#include <stdio.h>
#include <ctype.h>

void convertToLower(char *s) {

    while (*s != '\0') {
        *s = tolower((unsigned char)*s);
        s++;
    }

}

int main() {

    char str[100];

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    convertToLower(str);

    printf("Lowercase string: %s", str);

    return 0;

}

This method makes the code cleaner and allows easy reuse for multiple strings.

Program 6: Using Pointer Increment with tolower()

Pointer arithmetic combined with tolower() makes the conversion concise and elegant.

#include <stdio.h>
#include <ctype.h>

int main() {

    char str[100], *p;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (p = str; *p != '\0'; p++) {
        *p = tolower((unsigned char)*p);
    }

    printf("Lowercase string: %s", str);

    return 0;

}

This approach traverses the string using pointers while leveraging the standard library for safety and readability.

FAQs

Answering common questions about converting strings to lowercase in C.

1. Can these methods handle empty strings?
Yes. All methods safely return an empty string if no characters are present.

2. Are these methods case-sensitive?
Yes. Only uppercase letters ('A''Z') are converted to lowercase. Other characters, including numbers and punctuation, remain unchanged.

3. How do I handle non-ASCII characters?
The ASCII-based methods work only for standard English letters. For extended characters or Unicode, you may need locale-aware functions like towlower() from <wctype.h>.

4. Which method is most efficient?

  • Loop and pointer arithmetic methods are fast for small to medium strings.
  • Using tolower() is safe and readable, and performance is similar.
  • Recursion is elegant but less efficient for long strings due to stack usage.

5. Can I reuse these methods in other programs?
Yes. Function-based approaches (like Program 5) are modular and easy to integrate into larger projects or multiple strings.

Conclusion

Converting strings to lowercase without using strlwr() is simple once you understand ASCII values. You can achieve it using loops, pointers, or recursion. The loop method is straightforward, pointers make the code elegant, and recursion adds a different way of thinking about the problem.

While library functions are convenient in real projects, writing your own solutions strengthens your understanding of C programming and prepares you for situations where library support is limited.

References & Additional Resources

A curated collection of textbooks, tutorials, and documentation for learning string manipulation, character handling, and case conversion in C.

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language, 2nd Edition, Prentice Hall, 1988 – The foundational text covering strings, character functions, arrays, and core C programming concepts.
  2. ASCII Table Reference – Useful for understanding character codes, case conversions, and comparisons in C.
  3. Tutorialspoint: C Strings – Overview of string declaration, initialization, and operations in C.
  4. Cprogramming.com: Strings in C – Guide on string manipulation, case conversion, and related string algorithms in C.
Scroll to Top