Introduction:

This C program is designed to convert the case of letters in a string provided by the user. It reads a string input and transforms all lowercase letters to uppercase and all uppercase letters to lowercase. This can be useful for text manipulation, formatting, and various applications in data processing.

#include <stdio.h>
#include <ctype.h>
 
int main ()
{
    char sentence[100];
    int count, ch, i;
 
    printf("Enter a sentence \n");
    for (i = 0;(sentence[i] = getchar()) != '\n'; i++)
    {
        ;
    }
    sentence[i] = '\0';
    /*  shows the number of chars accepted in a sentence */
    count = i;
    printf("The given sentence is   : %s", sentence);
    printf("\n Case changed sentence is: ");
    for (i = 0; i < count; i++)
    {
        ch = islower(sentence[i])? toupper(sentence[i]) :
tolower(sentence[i]);
        putchar(ch);
    }
}

Include Header Files:

  • #include <stdio.h>: This header file is included for standard input/output functions like printf and fgets.
  • #include <ctype.h>: This header file is included for character classification and case conversion functions like islower, isupper, tolower, and toupper.

Function to Convert Case:

  • void convertCase(char *str): This function takes a string (character array) as input.

Loop Through the String:

  • for (int i = 0; str[i] != '\0'; i++): This loop iterates through each character of the string until it reaches the null terminator ('\0'), which marks the end of the string.

Check Character Case:

  • if (islower(str[i])): This condition checks if the current character is a lowercase letter.
    • If true, it converts the character to uppercase using str[i] = toupper(str[i]);.
  • else if (isupper(str[i])): This condition checks if the current character is an uppercase letter.
    • If true, it converts the character to lowercase using str[i] = tolower(str[i]);.

Main Function:

  • char str[100];: Declares a character array to hold the input string.
  • printf("Enter a string: ");: Prompts the user for input.
  • fgets(str, sizeof(str), stdin);: Reads a line of text, allowing spaces, and stores it in str.
  • str[strcspn(str, "\n")] = 0;: Removes the newline character added by fgets.
  • The original string is printed with printf("String before case conversion: %s\n", str);.
  • The convertCase function is called to convert the case of the characters in the string.
  • Finally, the modified string is printed: printf("String after case conversion: %s\n", str);.

Return Statement:

  • return 0;: Indicates successful completion of the program.

Input/Output Block:

Input:

  • The user is prompted to enter a string, which can contain letters and spaces.

Example Input:

Hello World!

Output:

  • The program outputs the string with the cases converted.

Example Output:

hELLO wORLD!

How to Run:

  1. Copy the code into a C compiler or IDE.
  2. Compile the program.
  3. Run the executable and input a string when prompted.
  4. View the converted output.

Conclusion

The case conversion program efficiently transforms lowercase letters to uppercase and vice versa in a given string. This functionality is useful for text formatting and manipulation tasks. By employing basic character checks and arithmetic operations, the program demonstrates fundamental string handling techniques in C, making it a valuable tool for developers needing to modify text data.

Leave a Reply

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

Verified by MonsterInsights