C Program to Convert a String to Lowercase

Introduction

Converting a string to lowercase involves changing all the uppercase characters in the string to their corresponding lowercase characters. This guide will show you how to write a C program to convert a string to lowercase.

Problem Statement

Create a C program that:

  • Takes a string as input from the user.
  • Converts all uppercase characters in the string to lowercase.
  • Displays the converted string.

Example:

  • Input: String = "Hello, World!"
  • Output: Lowercase String = "hello, world!"

Solution Steps

  1. Include the Standard Input-Output and Character Handling Libraries: Use #include <stdio.h> for standard input-output functions and #include <ctype.h> for character handling functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare a variable to store the input string.
  4. Input the String: Use gets or scanf to take input from the user for the string.
  5. Convert the String to Lowercase: Use a loop to iterate through each character of the string and convert it to lowercase using the tolower function from ctype.h.
  6. Display the Lowercase String: Use printf to display the converted string.

C Program to Convert String to Lowercase

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

int main() {
    // Step 1: Declare a variable to hold the input string
    char str[100];
    int i;

    // Step 2: Prompt the user to enter a string
    printf("Enter a string: ");
    gets(str);  // Using gets to read the string including spaces

    // Step 3: Convert the string to lowercase
    for (i = 0; str[i] != '\0'; i++) {
        str[i] = tolower(str[i]);
    }

    // Step 4: Display the lowercase string
    printf("Lowercase string: %s\n", str);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable str is declared to store the input string. The variable i is used as a loop counter.

Step 2: Input the String

  • The program prompts the user to enter a string using printf. The gets function is used to read the string, allowing it to include spaces.

Step 3: Convert the String to Lowercase

  • The program uses a for loop to iterate through each character of the string:
    • The tolower function from the ctype.h library converts each character to its lowercase equivalent if it is an uppercase letter.

Step 4: Display the Lowercase String

  • After converting the string to lowercase, the program displays the converted string using the printf function.

Step 5: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter a string: Hello, World!
Lowercase string: hello, world!

Example 2:

Enter a string: C Programming Is FUN!
Lowercase string: c programming is fun!

Conclusion

This C program demonstrates how to convert a string to lowercase by iterating through each character and using the tolower function. It covers basic concepts such as string manipulation, loops, and character handling, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top