C Program to Convert a String to Uppercase Without strupr()

C Program to Convert a String to Uppercase Without strupr()

Converting a string to uppercase is a common task in C programming. Normally, you can use the strupr() function, but it is not part of the C standard library and may not be available in all compilers. For this reason, it is useful to know how to convert a string to uppercase manually.

Understanding how to change lowercase letters into uppercase also helps you learn more about ASCII values and how characters are stored in memory. In the ASCII table, lowercase letters 'a' to 'z' have values from 97 to 122, while uppercase letters 'A' to 'Z' range from 65 to 90. The difference between a lowercase letter and its uppercase version is 32. By subtracting 32 from a lowercase letter, you can convert it to uppercase.

In this article, we will write different programs to convert a string into uppercase without using strupr(). We will cover approaches using loops, pointers, and recursion.

Understanding the Problem

The task is to take a string and change every lowercase character into its uppercase version. For example:

  • "hello world""HELLO WORLD"
  • "C programming""C PROGRAMMING"
  • "Already UPPER""ALREADY UPPER"

Non-alphabetic characters such as spaces, numbers, or symbols should remain unchanged.

Program 1: Using a Loop

The most straightforward method is to loop through the string and check each character. If it is between 'a' and 'z', convert it to uppercase.

#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("Uppercase string: %s", str);

    return 0;

}

This program is simple and shows clearly how ASCII values are used to perform the conversion.

Program 2: Using Pointers

Instead of array indexing, we can move through the string using pointers. This makes the code more elegant and efficient.

#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("Uppercase string: %s", str);

    return 0;

}

Here, the pointer moves from the first character to the null terminator, converting characters along the way.

Program 3: Using Recursion

We can also use recursion to process one character at a time. The function checks the first character, converts it if necessary, and then calls itself with the next character.

#include <stdio.h>

void toUppercase(char *s) {

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

    if (*s >= 'a' && *s <= 'z') {
        *s = *s - 32;
    }

    toUppercase(s + 1);

}

int main() {

    char str[100];

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

    toUppercase(str);

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

    return 0;

}

This method is less efficient for large strings but shows a different way to solve the problem.

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

The C standard library provides the toupper() function, which automatically converts a lowercase character to uppercase. This makes the code simpler and more readable.

#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] = toupper((unsigned char)str[i]);
    }

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

    return 0;

}

Here, toupper() handles only lowercase letters, leaving other characters unchanged.

Program 5: Using a Function for Modularity

We can wrap the uppercase logic in a separate function to make the code reusable and cleaner.

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

void convertToUpper(char *s) {

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

}

int main() {

    char str[100];

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

    convertToUpper(str);

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

    return 0;

}

This version emphasizes modularity and makes it easy to convert multiple strings without rewriting logic.

Program 6: Using Pointer Increment with toupper()

A combination of pointer arithmetic and the toupper() function can make 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 = toupper((unsigned char)*p);
    }

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

    return 0;

}

This highlights pointer traversal while leveraging the standard library function for safety and readability.

FAQs

Answering common questions about converting strings to uppercase in C.

1. Can these methods handle empty strings?
Yes. All methods will safely return an empty string if the input has no characters.

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

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

4. Which method is most efficient?

  • Loop and pointer arithmetic methods are fast and simple.
  • Using toupper() is safe, readable, and performs similarly.
  • Recursion is elegant but less efficient for long strings due to repeated function calls.

5. Can I reuse these methods in other programs?
Yes. Function-based methods (like Program 5) are modular and can be reused for multiple strings or integrated into larger projects.

Conclusion

Converting a string to uppercase without using strupr() is a simple yet important exercise in C. You can do it with loops, pointers, or recursion. While the loop and pointer methods are most practical, recursion helps you think about string operations differently.

In real-world programming, you would normally use library functions, but understanding how to do this manually strengthens your programming fundamentals and prepares you for coding interviews.

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