Program 58 - CSU1128 - CSE 2026 - Shoolini University

Program to convert a sentence entered by the user to uppercase without using any external library

Explanation

The program converts a sentence entered by the user to uppercase without using any external library functions.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and a character array is declared:

  • char sentence[256];: Array to hold the input sentence with a maximum length of 255 characters plus the null terminator.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter a sentence:

  • printf("Enter a sentence: ");: Prompts the user to enter a sentence.
  • scanf("%[^\n]", sentence);: Reads the sentence including spaces and stores it in the array sentence.

Convert to Uppercase

The program converts the sentence to uppercase:

  • for (int i = 0; sentence[i] != '\0'; i++): Loops through each character of the sentence until the null terminator is reached.
  • if (sentence[i] >= 'a' && sentence[i] <= 'z'): Checks if the current character is a lowercase letter.
  • sentence[i] = sentence[i] - 'a' + 'A';: Converts the lowercase letter to an uppercase letter by adjusting its ASCII value.

Output Result

The program prints the converted sentence:

  • printf("Converted sentence: %s\n", sentence);: Prints the sentence after converting it to uppercase.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    printf("\n\n Program to convert a sentence entered by the user to uppercase without using any external library. \n\n");

    // Declare a string to hold the input sentence
    char sentence[256];

    // Read the sentence from the user
    printf("Enter a sentence: ");
    scanf("%[^\n]", sentence);

    // Convert the sentence to uppercase
    for (int i = 0; sentence[i] != '\0'; i++)
    {
        if (sentence[i] >= 'a' && sentence[i] <= 'z')
        {
            sentence[i] = sentence[i] - 'a' + 'A';
        }
    }

    // Print the converted sentence
    printf("Converted sentence: %s\n", sentence);

    return 0;
}
                

Output

Program to convert a sentence entered by the user to uppercase without using any external library.

Enter a sentence: This is a DeMo sentence.
Converted sentence: THIS IS A DEMO SENTENCE