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.
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:
- It includes the standard input/output library.
- It defines a function
convertCase
that takes a string as input and converts all lowercase characters to uppercase and all uppercase characters to lowercase. - In the
main
function, it declares a character arraystr
of size 100 to store the input sentence. - It prompts the user to enter a sentence and reads the input using
fgets
. - It calls the
convertCase
function to convert the case of the characters in the sentence. - It prints the sentence after converting the case.
- It returns 0 to indicate successful execution of the program.
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
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.