files..

i want to read words from a file.
if letters of each word are upper convert to lower.(like A --> a)
if it is not a letter (like:!) convert to " "!" "

example: THE Cat! (i want to convert it to-->) the cat !



Last edited on
where is the problem?
Can you read a string from a file? if not, see http://www.cplusplus.com/doc/tutorial/files/
Then you have to iterate through the string and check whether it is in range form 'A' to 'Z'. If so subtract ('A'-'a') from it (there is a function to do that - http://www.cplusplus.com/reference/clibrary/cctype/tolower/ but I think a manual way is better when learning stuff). Also, if the letter is neither in range form 'A' to 'Z' nor from 'a' to 'z' set it to '!'
You can also use isupper() to check if it is upper case. And i would advise using the tolower() function than you're own version.
I answered you in the other topic, I attached code. ;)
I agree with Mcleano, the isupper() and tolower() commands are a very efficient way of changing case.
http://www.cplusplus.com/reference/std/locale/isupper/
Last edited on
bitwise operators case change at the system level, however I suppose additional code should be written for special or null characters

To uppercase:
The value 223 used in the AND statement is the decimal representation of 1101 1111. The operation will leave all bits unchanged except for the 6th bit, which will be set to 0; lowercase letters are the same as the uppercase except the lowercase ones are greater than uppercase by a value of 32 so to change from lowercase to uppercase, just turn the 6th bit off.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Uppercase letters using bitwise AND '&'

#include <iostream>
using namespace std;


int main()
{

  char ch;

  for (int i=0;i<10;i++) {
    ch = 'a' + i;
    cout << ch;

    // This statement turns off the 6th bit
    ch = ch & 223; // ch is now uppercase
    cout << ch;
  }

  cout << "\n";
return 0;

}
.

aAbBcCdDeEfFgGhHiIjJ


To lowercase:
the bitwise OR , as the reverse of AND can be used to turn bits on, any bits that are set to 1 in either operand will cause that bit turn on, the value used in the arguement would be 32(this turns the 6th bit on)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Lowercase letters using bitwise OR '|'

#include <iostream>
using namespace std;


int main()
{

  char ch;

  for (int i=0;i<10;i++) {
    ch = 'A' + i;
    cout << ch;

    // This statement turns on the 6th bit
    ch = ch | 32; // ch is now lowercase
    cout << ch;
  }

  cout << "\n";
return 0;

}

AaBbCcDdEeFfGgHhIiJj


Last edited on
What if the system uses EBCDIC?
Topic archived. No new replies allowed.