create account

Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly) by charlie.wilson

View this thread on: hive.blogpeakd.comecency.com
· @charlie.wilson ·
$51.87
Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)
<center>http://66.media.tumblr.com/0abe21b481c44032d3c4e0beedf6759b/tumblr_inline_neu6fx1e1F1t32je4.jpg
[Image source](http://luxloop.tumblr.com/post/102336117692/on-art-and-coding)</center>

Hey guys! Here is a quick little programming tip about going from uppercase to lowercase, or lowercase to uppercase in a C or C++ string. Honestly, you can change a character to whatever character you want if you know the ASCII values. Here is an ASCII table:

http://www.asciitable.com/index/asciifull.gif
[Image source](http://www.asciitable.com)

We are comparing the "Dec" column to the "Chr" column (decimal to character). Notice how each corresponding decimal number of an uppercase letter is 32 less than the number of its lowercase match. 

So to change a letter from uppercase to lowercase, all that we need to do is add 32:
```
char c = 'A';
printf("%c\n", c);
c += 32;
printf("%c\n", c);
```
Output:
```
A
a
```

Going the other way:
```
char c = 'a';
printf("%c\n", c);
c -= 32;
printf("%c\n", c);
```
Output:
```
a
A
```

It is that simple! Here is a program I wrote that changes all lowercase values in a string to uppercase:
```
#include<stdio.h>
#include<string.h>

int main()
{
	printf("Enter a sentence (50 char max): ");
	char input[50];
	fgets(input, 50, stdin);
	int x;
	for(x = 0; x < strlen(input); x++)
		if(input[x] > 96 && input[x] < 123)    //    only change if it is lowercase -- from 97 to 122
			input[x] -= 32;
	printf("%s", input);
	return 0;
}
```
Output:
```
[cmw4026@omega test]$ gcc upper.c
[cmw4026@omega test]$ ./a.out
Enter a sentence (50 char max): I like EATING dogs?
I LIKE EATING DOGS?
[cmw4026@omega test]$
```

I hope that was useful! It is important to understand how ASCII characters work in my opinion. Leave any suggestions in the comments.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 34 others
properties (23)
authorcharlie.wilson
permlinkeasy-programming-tip-change-a-string-to-uppercase-lowercase-ascii-c-and-c-beginner-friendly
categoryprogramming
json_metadata{"tags":["programming","tutorial","beginner","ascii","characters"],"image":["http://66.media.tumblr.com/0abe21b481c44032d3c4e0beedf6759b/tumblr_inline_neu6fx1e1F1t32je4.jpg","http://www.asciitable.com/index/asciifull.gif"],"links":["http://luxloop.tumblr.com/post/102336117692/on-art-and-coding","http://www.asciitable.com"]}
created2016-10-18 05:42:42
last_update2016-10-18 05:42:42
depth0
children5
last_payout2016-11-18 09:38:30
cashout_time1969-12-31 23:59:59
total_payout_value39.951 HBD
curator_payout_value11.920 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,781
author_reputation18,779,183,062,076
root_title"Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,562,053
net_rshares55,102,240,452,399
author_curate_reward""
vote details (98)
@qagiri ·
@charlie.wilson
Playing around with ASCII is cool
Thanks for sharing this.
👍  
properties (23)
authorqagiri
permlinkre-charliewilson-easy-programming-tip-change-a-string-to-uppercase-lowercase-ascii-c-and-c-beginner-friendly-20161018t060354347z
categoryprogramming
json_metadata{"tags":["programming"],"users":["charlie.wilson"]}
created2016-10-18 06:06:33
last_update2016-10-18 06:06:33
depth1
children1
last_payout2016-11-18 09:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length74
author_reputation5,207,321,068,642
root_title"Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,562,126
net_rshares6,959,489,948
author_curate_reward""
vote details (1)
@charlie.wilson ·
No problem, thanks for reading!
properties (22)
authorcharlie.wilson
permlinkre-qagiri-re-charliewilson-easy-programming-tip-change-a-string-to-uppercase-lowercase-ascii-c-and-c-beginner-friendly-20161018t065033478z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-18 06:50:33
last_update2016-10-18 06:50:33
depth2
children0
last_payout2016-11-18 09:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length31
author_reputation18,779,183,062,076
root_title"Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,562,365
net_rshares0
@thoughts-in-time ·
Would it be difficult to program a Pidgin plugin that sends the text displayed to the windows clipboard? It would be cool if pidgin could talk, and most TTS has a clipboard monitor.
👍  
properties (23)
authorthoughts-in-time
permlinkre-charliewilson-easy-programming-tip-change-a-string-to-uppercase-lowercase-ascii-c-and-c-beginner-friendly-20161018t064211638z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-18 06:42:12
last_update2016-10-18 06:42:12
depth1
children2
last_payout2016-11-18 09:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length181
author_reputation59,797,459,104,031
root_title"Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,562,303
net_rshares6,959,489,948
author_curate_reward""
vote details (1)
@charlie.wilson ·
Probably not, but I have never tried it... It sounds simple enough.
👍  
properties (23)
authorcharlie.wilson
permlinkre-thoughts-in-time-re-charliewilson-easy-programming-tip-change-a-string-to-uppercase-lowercase-ascii-c-and-c-beginner-friendly-20161018t065120922z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-18 06:51:21
last_update2016-10-18 06:51:21
depth2
children1
last_payout2016-11-18 09:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length67
author_reputation18,779,183,062,076
root_title"Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,562,370
net_rshares466,301,925
author_curate_reward""
vote details (1)
@thoughts-in-time ·
If it seems interesting I'd be a willing guinea pig to try it out. It also could make for a fun programming post.
properties (22)
authorthoughts-in-time
permlinkre-charliewilson-re-thoughts-in-time-re-charliewilson-easy-programming-tip-change-a-string-to-uppercase-lowercase-ascii-c-and-c-beginner-friendly-20161018t070317459z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-10-18 07:03:18
last_update2016-10-18 07:03:18
depth3
children0
last_payout2016-11-18 09:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length113
author_reputation59,797,459,104,031
root_title"Easy Programming Tip -- change a string to uppercase/lowercase (ASCII) C and C++ (beginner friendly)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,562,431
net_rshares0