Write a program that accepts a lower case character and use a function to convert this character to upper case. Display the converted character on the screen.
are you familiar with the ascii table?
http://www.hextoascii.info/img/ascii_chart-ascii_to_hex_binary_decimal-table.png notice that in binary, the difference between upper and lower case letters is just one bit (it happens to be the 32 bit, but you shouldn't hard-code the number) using this fact you can either use a bit-wise operator like | (or), & (and), or ~ (not), or a series of type conversions, to exploit that difference and change that bit here's a tutorial on bitwise operators if you aren't familiar with them http://www.cprogramming.com/tutorial/bitwise_operators.html
#include<stdio.h> void conversion(char c); int main() { char ch, result; Printf("Enter a character:"); while(islower(ch) scanf("%c", &ch); result=conversion(ch); printf("Result is %c\n", result); return 0; void conversion(char c) { char letter=toupper(c); return letter; } @TuringTest : can you correct my code pls? thnx
well for one thing you have a "while" statement with no braces to indicate the body, and your while condition is while(islower(ch)), which I think is not a very good while condition. (actually, I don't even see why you want a while loop, when is your program supposed to terminate?). In any event, islower is not a standard C function or in stdio.h. if you want to use that function you need to include the ctype.h library. Is your program supposed to keep taking input from the user, or just change one character?
also your function "conversions" says it will return a void type, but you return a character. If you want to keep the function as having a void return type, you need to pass in the character argument by reference, otherwise change the return type to char
#include<stdio.h> void conversion(char c); int main() { char ch, result; Printf("Enter a character:"); //printf is not capitalized while(islower(ch) //ch is uninitialized since you have not yet called scanf, // you're missing parentheses and braces, and islower scanf("%c", &ch); // is part of the ctype.h library which you have not included result=conversion(ch); printf("Result is %c\n", result); return 0; void conversion(char c) { char letter=toupper(c); return letter; //function prototype and declaration state the return type as void, } //but here you return type char
I haven't run this, but something like #include<stdio.h> #include<ctype.h> char conversion(char c); int main() { char ch, result; printf("Enter a character:"); scanf("%c", &ch); if(islower(ch)) { result=conversion(ch); printf("Result is %c\n", result); } return 0; } char conversion(char c) { return toupper(c); } should take in one input and change it to uppercase, though this whole program could be written without the help of your auxiliary function "conversion". I'm not sure exactly what the specifications are on your assignment are though. I suggest you try to cut a lot of the fat out of this code; it could probably be done in about 4 lines total, and you don't need the functions islower/toupper if you use a little ascii magic ;)
@TuringTest ......thank you so much, you code is good. You are a genius man.
Can you help me in this code too: Write a program containing a function that takes has 2 integer parameters x and y and returns the value of x^2 + y^2. The main program should then allow the input of two integer values a and b and display the value of a^2+b^2. Write in C programming.
#include <stdio.h> int quadratic(int x, int y); int main() { int a, b, result; printf("pls enter an integer value for a:"); scanf("%d", &a); printf("pls enter an integer value for b:"); scanf("%d", &b); result=quadratic(a,b); printf("Result is "%d", result); } int quadratic(int x, int y) { int z; z = (x*x) + (y*y); return z; }
(in the future please post each question separately) that one looks fine to me, what problem are you having with it? what output is it giving you?
I should point out that you don't need the automatic variable z in your quadratic function, you could simply put int quadratic(int x, int y) { return x*x + y*y; } but your code does the same thing, it just wastes time creating the variable z.
thank you TuringTest for you help.
very welcome!
Join our real-time social learning platform and learn together with your friends!