Reputation: 113
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;
}
Upvotes: 5
Views: 132292
Reputation: 45
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 | ' ';
}
Upvotes: 1
Reputation: 136
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;
}
Upvotes: 1
Reputation: 311
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();
}
Upvotes: -1
Reputation: 1
#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();
}
Upvotes: -2
Reputation: 1016
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;
}
Upvotes: 4
Reputation: 1884
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 :)
Upvotes: 2
Reputation: 100
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;
}
Upvotes: 0
Reputation: 14
#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
Upvotes: -1
Reputation: 3542
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'));
}
Upvotes: 23
Reputation: 613
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/
Upvotes: 4