Lower and Upper Case in C language

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2294 Views - Last Post: 06 June 2013 - 02:43 PM Rate Topic: -----

#16 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Lower and Upper Case in C language

Posted 04 June 2013 - 09:08 PM

You are welcome. Tell us how you ended up solving the problem of keeping the order of the letters as they came in without using an array.
Was This Post Helpful? 0
  • +
  • -

#17 Dessen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 13

Re: Lower and Upper Case in C language

Posted 05 June 2013 - 07:36 PM

I am new to bit shifting so don't know how to manipulate them very well. Now to output each bit from the variable int flag.

int i;

for(i = 0; i < 26; i++)
{
if((flag & 1) >= 1)

{

printf("%c", i + 'a');

}
}

Would this work? AND my flag with 1, and if true print i + 'a'?

I am new to bit shifting so don't know how to manipulate them very well. Now to output each bit from the variable int flag.

int i;
for(i = 0; i < 26; i++)
{
if((flag & 1) >= 1)

{

printf("%c", i + 'a');

}
}


Would this work? AND my flag with 1, and if true print i + 'a'?
Was This Post Helpful? 0
  • +
  • -

#18 Dessen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 13

Re: Lower and Upper Case in C language

Posted 05 June 2013 - 07:44 PM

Sorry I meant AND it with 2 to the power of 1,2,3,4,5,6
Was This Post Helpful? 0
  • +
  • -

#19 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Lower and Upper Case in C language

Posted 05 June 2013 - 08:03 PM

Quote

printf("%c", i + 'a'); 



Since the information is held in the flag variable, your print statement should the flag variable don't you think?

To print each bit in an integer, loop over the total number of bits in the flag (0 - sizeof(int) * 8). For each iteration, shift the flag variable to the right by the value of 'i', then bitwise-and the result with 1.

i.e.
#define BITS_PER_BYTE 8

int flag = 0x22; //binary = 0000 0000 0010 0010 (32 bit integer)

for(int i=0; i<sizeof(int) * BITS_PER_BYTE; i++) {
   printf("%d, (flag >> i) & 1);
}



The effect of bitwise-anding the result of (flag >> i) makes all the other bits 0, except the one that is of current intrest.

You will notice that the code above, when run, will print the binary backwards (most significant bit last). This is because we start print the least significant bit first (flag >> 0) & 1.

This post has been edited by jjl: 05 June 2013 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#20 Dessen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-June 13

Re: Lower and Upper Case in C language

Posted 05 June 2013 - 11:23 PM

View Postjjl, on 05 June 2013 - 08:03 PM, said:

Quote

printf("%c", i + 'a'); 



Since the information is held in the flag variable, your print statement should the flag variable don't you think?

To print each bit in an integer, loop over the total number of bits in the flag (0 - sizeof(int) * 8). For each iteration, shift the flag variable to the right by the value of 'i', then bitwise-and the result with 1.

i.e.
#define BITS_PER_BYTE 8

int flag = 0x22; //binary = 0000 0000 0010 0010 (32 bit integer)

for(int i=0; i<sizeof(int) * BITS_PER_BYTE; i++) {
   printf("%d, (flag >> i) & 1);
}



The effect of bitwise-anding the result of (flag >> i) makes all the other bits 0, except the one that is of current intrest.

You will notice that the code above, when run, will print the binary backwards (most significant bit last). This is because we start print the least significant bit first (flag >> 0) & 1.


I understand what you are saying, but I have to print the character that is mapped to the bit. Which is why my printf had a char output. Which is why I used i + 'a' to output the character corresponding to the current bit.

My goal was to check if the bit is 1 or 0. If it is 1 then output the alphabet thats mapped to it.
Was This Post Helpful? 0
  • +
  • -

#21 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Lower and Upper Case in C language

Posted 06 June 2013 - 02:43 PM

Quote

My goal was to check if the bit is 1 or 0. If it is 1 then output the alphabet thats mapped to it.

Well you know how to get the status of each bit, I showed it in the previous post.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2