Knowee
Questions
Features
Study Tools

Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa.

Question

Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that reads a sentence and replaces lowercase characters with uppercase and vice versa.

#include <stdio.h>

void convertCase(char *str) {
    int i = 0;
    while(str[i] != '\0') {
        if(str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        }
        else if(str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        }
        i++;
    }
}

int main() {
    char str[100];
    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);
    convertCase(str);
    printf("After converting case: %s", str);
    return 0;
}

This program works as follows:

  1. It includes the standard input/output library.
  2. It defines a function convertCase that takes a string as input and converts all lowercase characters to uppercase and all uppercase characters to lowercase.
  3. In the main function, it declares a character array str of size 100 to store the input sentence.
  4. It prompts the user to enter a sentence and reads the input using fgets.
  5. It calls the convertCase function to convert the case of the characters in the sentence.
  6. It prints the sentence after converting the case.
  7. It returns 0 to indicate successful execution of the program.

This problem has been solved

Similar Questions

Write a C program to read an English Alphabet through keyboard and display whetherthe given alphabet is in upper case or lower case.

Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.

Write a program in C to print a string in reverse using a character pointer.Also print the uppercase of the first letter of the reversed word.

Write a C program to read a sentence through keyboard and to display the count ofwhite spaces in the given sentence.

Write a C program to check whether an alphabet is vowel or consonant using switch case

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.