Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Program to convert string to uppercase or lowercase

Write a program that takes a string as input and convert it to uppercase or lowercase.

Program in C

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

void convertToUppercase(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
}

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

int main() {
    char input[100];
    char choice;

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

    // Remove trailing newline character
    input[strcspn(input, "\n")] = '\0';

    printf("Enter 'U' to convert to uppercase or 'L' to convert to lowercase: ");
    scanf(" %c", &choice);

    if (choice == 'U' || choice == 'u') {
        convertToUppercase(input);
        printf("Uppercase string: %s\n", input);
    } else if (choice == 'L' || choice == 'l') {
        convertToLowercase(input);
        printf("Lowercase string: %s\n", input);
    } else {
        printf("Invalid choice. Please enter 'U' or 'L'.\n");
    }

    return 0;
}

Explanation:

  • In this program:
  • We declare a character array input to store the user’s input string and a character variable choice to store the user’s conversion choice.
  • The fgets function is used to read the user’s input, with a limit on the input length to prevent buffer overflow.
  • We remove the trailing newline character from the input string using strcspn function.
  • The user is prompted to enter ‘U’ to convert the string to uppercase or ‘L’ to convert it to lowercase.
  • If the user’s choice is ‘U’ or ‘u’, the convertToUppercase function is called to convert the string to uppercase using the toupper function.
  • If the user’s choice is ‘L’ or ‘l’, the convertToLowercase function is called to convert the string to lowercase using the tolower function.
  • Finally, the converted string is printed to the console.

Output:

C
Enter a string: EasyExamNotes
Enter 'U' to convert to uppercase or 'L' to convert to lowercase: U
Uppercase string: EASYEXAMNOTES

Program in Java

Java
import java.util.Scanner;

public class StringCaseConversion {
    public static void convertToUppercase(char[] str) {
        for (int i = 0; i < str.length; i++) {
            str[i] = Character.toUpperCase(str[i]);
        }
    }

    public static void convertToLowercase(char[] str) {
        for (int i = 0; i < str.length; i++) {
            str[i] = Character.toLowerCase(str[i]);
        }
    }

    public static void main(String[] args) {
        char[] input = new char[100];
        char choice;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");
        input = scanner.nextLine().toCharArray();

        // Remove trailing newline character
        input[input.length - 1] = '\0';

        System.out.print("Enter 'U' to convert to uppercase or 'L' to convert to lowercase: ");
        choice = scanner.next().charAt(0);

        if (choice == 'U' || choice == 'u') {
            convertToUppercase(input);
            System.out.println("Uppercase string: " + new String(input));
        } else if (choice == 'L' || choice == 'l') {
            convertToLowercase(input);
            System.out.println("Lowercase string: " + new String(input));
        } else {
            System.out.println("Invalid choice. Please enter 'U' or 'L'.");
        }
    }
}

Explanation:

  1. import java.util.Scanner;: This line imports the Scanner class from the java.util package. It is used for reading user input.
  2. public class StringCaseConversion: This line declares a public class named StringCaseConversion.
  3. public static void convertToUppercase(char[] str): This is a static method that takes a character array str as a parameter and converts all characters to uppercase using Character.toUpperCase().
  4. public static void convertToLowercase(char[] str): This is a static method that takes a character array str as a parameter and converts all characters to lowercase using Character.toLowerCase().
  5. public static void main(String[] args): This is the main method that serves as the entry point of the program.
  6. char[] input = new char[100];: This line declares a character array named input to store the user input.
  7. Scanner scanner = new Scanner(System.in);: Creates a new Scanner object named scanner to read user input from the console.
  8. input = scanner.nextLine().toCharArray();: Reads the input string using scanner.nextLine() and converts it to a character array using toCharArray().
  9. input[input.length – 1] = ‘\0’;: Removes the trailing newline character by replacing it with the null character (‘\0’).
  10. choice = scanner.next().charAt(0);: Reads the user’s choice character using scanner.next().charAt(0).
  11. if (choice == ‘U’ || choice == ‘u’) { … } else if (choice == ‘L’ || choice == ‘l’) { … } else { … }: This conditional statement checks the user’s choice and performs the corresponding case conversion operation or displays an error message for an invalid choice.
  12. System.out.println(“Uppercase string: ” + new String(input)); and System.out.println(“Lowercase string: ” + new String(input));: These lines convert the character array input back to a string using new String(input) and print the resulting uppercase or lowercase string.
Output
Enter a string: EasyExamNotes
Enter 'U' to convert to uppercase or 'L' to convert to lowercase: U
Uppercase string: EASYEXAMNOTE