Knowee
Questions
Features
Study Tools

Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa without using fgets function and without using the pointer

Question

Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa without using fgets function and without using the pointer.

🧐 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. This program uses the getchar() function to read the characters one by one and the putchar() function to print the characters. It doesn't use the fgets function or pointers.

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

int main() {
    char ch;

    printf("Enter a sentence: ");

    while ((ch = getchar()) != '\n') {
        if (islower(ch)) {
            putchar(toupper(ch));
        } else if (isupper(ch)) {
            putchar(tolower(ch));
        } else {
            putchar(ch);
        }
    }

    return 0;
}

Here's how it works:

  1. The program first includes the necessary header files: stdio.h for input/output functions and ctype.h for character handling functions.

  2. The main function starts. It declares a character variable ch to hold the characters read from the input.

  3. The program then prints a prompt asking the user to enter a sentence.

  4. The program enters a while loop that continues until the user hits the Enter key (which sends a newline character '\n'). Inside the loop, the program reads a character from the input using the getchar() function and assigns it to ch.

  5. The program then checks if the character is a lowercase letter using the islower() function. If it is, the program converts the character to uppercase using the toupper() function and prints it using the putchar() function.

  6. If the character is not a lowercase letter, the program checks if it's an uppercase letter using the isupper() function. If it is, the program converts the character to lowercase using the tolower() function and prints it.

  7. If the character is neither a lowercase nor an uppercase letter (for example, it's a digit or a punctuation mark), the program simply prints it as it is.

  8. The main function 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 check whether an alphabet is vowel or consonant using switch case

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

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.