Monday, April 28, 2025

C Program - Convert Uppercase String to Lowercase String

This is a C program that converts uppercase letters in a string to lowercase letters.

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

int main()
{

    char some_string[100];
    int x;

    printf("Enter the String: \n");
    scanf("%s", some_string);

    for (x = 0; x <= strlen(some_string); x++)
    {
        if (some_string[x] >= 65 && some_string[x] <= 90)
        {
            some_string[x] = some_string[x] + 32;
            printf("Individual character: %d <--> %c \n", some_string[x], some_string[x]);
        }
    }

    printf("Lowercase: %s\n", some_string);

    return 0;
}

Result :

Enter the String:
SOMETHING
Individual character: 115 <--> s
Individual character: 111 <--> o
Individual character: 109 <--> m
Individual character: 101 <--> e
Individual character: 116 <--> t
Individual character: 104 <--> h
Individual character: 105 <--> i
Individual character: 110 <--> n
Individual character: 103 <--> g
Lowercase: something

Process returned 0 (0x0)   execution time : 4.172 s
Press any key to continue.

Let's go through the code line by line to understand how it works: 

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

These are header files that are needed to use standard input/output functions and string manipulation functions. 

int main()
{
    char some_string[100];
    int x;
    
    printf("Enter the String: \n");
    scanf("%s", some_string);

This is the main function of the program. It declares a character array called some_string of size 100 and an integer variable x. The printf function displays the message "Enter the String: " on the screen. The scanf function reads a string input from the user and stores it in the some_string array. 

    for (x = 0; x <= strlen(some_string); x++)
    {
        if (some_string[x] >= 65 && some_string[x] <= 90)
        {
            some_string[x] = some_string[x] + 32;
            printf("Individual character: %d <--> %c \n", some_string[x], some_string[x]);
        }
    }

This is a for loop that iterates through each character in the some_string array using an index variable x. The strlen function returns the length of the string in some_string.

The loop checks if the current character in some_string is an uppercase letter by comparing its ASCII value to the ASCII values of 'A' (65) and 'Z' (90). If the character is an uppercase letter, it is converted to lowercase by adding 32 to its ASCII value, which corresponds to the difference between the uppercase and lowercase letters in the ASCII table.

The printf function displays the individual character that has been converted to lowercase along with its ASCII code. 

    printf("Lowercase: %s\n", some_string);
    return 0;
}

After the for loop has finished iterating through the characters in the string, the final lowercase string is displayed using the printf function, and the program ends with a return value of 0.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...