This program is supposed to take a user input string and change lower case characters to upper case and vice versa. It works except that for spaces between words it puts @ and then it adds some weird characters at the end of every string. What is wrong?

You're unconditionally adding 32 to any character which isn't between 'a' and 'z', including spaces, numbers, punctuation and so on.

Also, in order of relative importance :

Fix the indentation of the code
Don't use magic numbers - use constants such as 'a', 'A', -'A'+'a' and so on.
You'd be better off using functions such as isuppper or islower to test for upper or lower case letters
gets is unsafe - look into using fgets(str, sizeof(str), stdin) instead.

11-03-2011

anduril462

Code:

if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
else
str[i]=str[i]+32;

So if str[i] is between 97 and 122 (lowercase), you subtract 32 to make it upper case. Then you have an else, which accounts for every other character that isn't lowercase, including spaces, punctuation, etc, and you add 32 to that to "make it lowercase".

Also, stop using magic numbers that are specific to ASCII (which not everybody uses). How about using functions like isupper, islower, toupper and tolower? Just #include <ctype.h>.

11-03-2011

KCfromNC

Jinx :)

11-03-2011

Interista

Quote:

Originally Posted by anduril462

Code:

if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
else
str[i]=str[i]+32;

So if str[i] is between 97 and 122 (lowercase), you subtract 32 to make it upper case. Then you have an else, which accounts for every other character that isn't lowercase, including spaces, punctuation, etc, and you add 32 to that to "make it lowercase".

Also, stop using magic numbers that are specific to ASCII (which not everybody uses). How about using functions like isupper, islower, toupper and tolower? Just #include <ctype.h>.

I absolutely agree. If there is something easier then its that that we should use. However, our lecturer has not taught it to us yet which is ridiculous.

11-03-2011

Interista

Perfect thanks!

11-03-2011

KCfromNC

Quote:

Originally Posted by Interista

I absolutely agree. If there is something easier then its that that we should use. However, our lecturer has not taught it to us yet which is ridiculous.

Nope, it's important to understand the concept of how characters map to integers and back again. Later is the time for shortcuts written by other people who already understand those concepts, but now's the time to make sure you become one of them.

11-03-2011

grumpy

Quote:

Originally Posted by anduril462

Also, stop using magic numbers that are specific to ASCII (which not everybody uses). How about using functions like isupper, islower, toupper and tolower? Just #include <ctype.h>.

If, for sake of discussion, we do not want to use those standard library functions and we are willing to limit our code for english speakers, we can use a property of all character sets.

All real-world character sets (ASCII, EDCDIC, etc) place the letters 'A' through 'Z' in sequence (so 'B' is equivalent to 'A' + 1, 'Z' is equivalent to 'A' + 25, etc) and the letters 'a' through 'z' also in sequence for all real-world character sets. Also, in all real-world character sets, the letters have positive values (i.e. neither 'A' nor 'a' are negative). The problem, however, is that the relationship between 'A' and 'a' (for example, number of characters intervening between them) does vary between character sets.

So, to avoid the magic numbers, it is possible to do this (for a conversion from upper to lower case).

This will work for any real-world character set. I've broken the code in the body of the if block into two lines to describe what is actually happening.

The limitation of this approach is that it is not necessarily applicable for natural languages other than english. The standard functions (isupper(), tolower(), etc) are "locality aware".

Quote:

Originally Posted by Interista

I absolutely agree. If there is something easier then its that that we should use. However, our lecturer has not taught it to us yet which is ridiculous.

As I discussed in your previous thread, you are speaking rubbish.

To make a point, this thread gives you a solution to one of your homework exercises. However, you might want to keep in mind that your lecturer probably expects you to learn by doing, whereas you expect to be spoon-fed.

11-03-2011

adeyblue

Quote:

Originally Posted by grumpy

All real-world character sets (ASCII, EDCDIC, etc) place the letters 'A' through 'Z' in sequence

It's the numbers that are guaranteed to be sequential, letters aren't. Certainly not in EBCDIC where, for example, I = 0xC9 and J = 0xD1

Quote:

Also, in all real-world character sets, the letters have positive values (i.e. neither 'A' nor 'a' are negative)