By Ahmed

2020-05-22 23:08:56 8 Comments

In this program, how i do at the same time convert the uppercase letters to lowercase letters and lowercase letters to uppercase letter?

I have tried many times but it is not working.

My expectations, Suppose for example


 from read.txt(orginal contant of the file:
 Hello World) 



This is my code.... (I can only convert from uppercase to lowercase. At the same time I could not convert from uppercase to lowercase and lowercase to uppercase).

int main() 

    FILE* file;
    char ch;  

    file = fopen("read.txt","r"); 

    while (ch != EOF) 
        ch = toupper(ch); 

        printf("%c", ch); 

        ch = fgetc(file); 
    return 0; 



@Milag 2020-05-23 18:33:25

// Note that UPPER and lower chars differ by bit 5 (value 0x20).
// If you want to switch case for any [A-Za-z] in one step,
// an exclusive-OR (^ bitwise operator) can be used:

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

#define TOGGLE_CASE(c)  ((c) ^ 0x20)

    FILE *file;
    char ch;  

    if ((file = fopen("read.txt", "r")) == NULL) {
        dprintf(2, "fopen error\n");
        return (1);

    while ((ch = fgetc(file)) != EOF)
        printf("%c", isalpha(ch) ? TOGGLE_CASE(ch) : ch);

    return (0); 

@P__J__ 2020-05-22 23:15:12

you have plenty errors here.

your first check the not initialized ch variable, you use it and try to print, then you read it. The order has to be right opposite.

ch has to be of type int to accommodate EOF

you need to check if the fopen was successful

int main() 
    FILE* fptr;
    int ch;  

    fptr = fopen("read.txt","r"); 

        while ((ch = fgetc(fptr)) != EOF) 
            ch = toupper(ch); 
            printf("%c", ch); 
    return 0; 

@tadman 2020-05-22 23:18:27

What's fptr? Not defined here. Meaning file?

@P__J__ 2020-05-22 23:19:45

@tadman It was OPs code. He did not even compile it.

@tadman 2020-05-22 23:21:44

Oh I get that, it's just another mistake that needs patching. Not your bad!

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

20 Answered Questions

[SOLVED] Accessing the index in 'for' loops?

  • 2009-02-06 22:47:54
  • Joan Venge
  • 2109999 View
  • 3596 Score
  • 20 Answer
  • Tags:   python loops list

39 Answered Questions

[SOLVED] Loop through an array in JavaScript

44 Answered Questions

[SOLVED] JavaScript closure inside loops – simple practical example

61 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

16 Answered Questions

[SOLVED] How do I copy a file in Python?

10 Answered Questions

41 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

28 Answered Questions

[SOLVED] How to read a file line-by-line into a list?

12 Answered Questions

[SOLVED] Looping through the content of a file in Bash

  • 2009-10-05 17:52:54
  • Peter Mortensen
  • 1607554 View
  • 1385 Score
  • 12 Answer
  • Tags:   linux bash loops unix io

Sponsored Content