5

A really simple program. I just want to turn an 'A' into an 'a', but output is giving me 'A'.

#include <stdio.h>

int main(void) {
    putchar(lower('A')); 

}

lower(a) 
int a; 
{
    if ((a >= 65) && (a >= 90))
        a = a + 32; 
    return a;  
}
lche
  • 113
  • 1
  • 3
  • 7

10 Answers10

23

You messed up the second part of your if condition. That should be a <= 90.

Also, FYI, there is a C library function tolower that does this already:

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

int main() {
    putchar(tolower('A'));
}
Scott Olson
  • 3,513
  • 24
  • 26
  • 4
    Be aware that `tolower` is locale-dependant though. It's better to use `return (c <= 'Z' && c >= 'A') ? c + 32 : c` if you know you only want ASCII semantics. – Craig Barnes May 05 '14 at 23:22
4

I believe you want <= 90

lower(a) 
int a; 
{
    if ((a >= 65) && (a <= 90))
        a = a + 32; 
    return a;  
}

Although tolower would probably just save you the hassle unless you wanted to do this yourself. http://www.cplusplus.com/reference/cctype/tolower/

spartacus
  • 613
  • 6
  • 12
  • For those wondering, `lower(a) int a;` is a very old style of function declaration. You should avoid it and use the standard `int lower(int a)` instead – Spikatrix Jul 05 '19 at 05:38
4

In ASCII the upper and lower case alphabet are 0x20 apart from each other, so this is another way to do it.

int lower(int a) 
{
    if ((a >= 0x41) && (a <= 0x5A))
        a |= 0x20; 
    return a;  
}
Dave Newman
  • 1,018
  • 10
  • 15
2

One way to make sure it is correct is by using character instead of ascii code.

if ((a >= 65) && (a <= 90))

what you want is to lower a case. it's better to use something like if (a >= 'A' && a <= 'Z') . You don't have to remind all ascii code :)

MLeblanc
  • 1,816
  • 12
  • 21
1

You can convert a character from lower case to upper case and vice-versa using bit manipulation as shown below:

#include<stdio.h>
int main(){
  char c;
  printf("Enter a character in uppercase\n");
  scanf("%c",&c);
  c|=' '; // perform or operation on c and ' '
  printf("The lower case of %c is \n",c);
  c&='_'; // perform 'and' operation with '_' to get upper case letter. 
  printf("Back to upper case %c\n",c);   
  return 0;
}
sachin_hg
  • 136
  • 3
1

In ASCII the upper and lower case alphabet is 0x20 (in ASCII 0x20 is space ' ') apart from each other, so this is another way to do it.

int lower(int a) 
{
    return a | ' ';  
}
0

If condition is wrong. Also return type for lower is needed.

#include <stdio.h>

int lower(int a)  
{
    if ((a >= 65) && (a <= 90))
        a = a + 32; 
    return a;  
}

int _tmain(int argc, _TCHAR* argv[])
{

    putchar(lower('A')); 
    return 0;
}
user1596193
  • 100
  • 3
-1
#include <stdio.h>
#include <string.h>

int main()
{
    char string[] = "Strlwr in C";

    printf("%s\n",strlwr(string));

    return  0;
}

use strlwr for lowering the case

-1

Use This code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
char a[10];
clrscr();
gets(a);
int i,length=0;
for(i=0;a[i]!='\0';i++)
length+=1;
for(i=0;i<length;i++){
a[i]=a[i]^32;
}
printf("%s",&a);
getch();
}
-2
#include<stdio.h>

void main()
{
 char a;

 clrscr();
 printf("enter a character:");
 scanf("%c",&a);

 if(a>=65&&a<=90)
   printf("%c",a+32);
 else
   printf("type a capital letter");

 getch();
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 5
    Adding an answer isn't enough. You should comment as to why this is right and why this works. Also try to format your answer a bit better. – parakmiakos Aug 04 '14 at 17:30