Arthur
Arthur

Reputation: 171

C - Want to convert lowercase and uppercase letters using symbols

I want to convert lowercase letters to uppercase and vice versa in C. I already do that but when I use a symbol like '_' or '.' or ',' .... on the input I get random character on the output like a question mark inside a square.

INPUT: AAbb
OUTPUT: aaBB
INPUT: a_A
OUTPUT: A⍰a

How can I make this work with symbols?

Code:

#include <stdio.h>

int main(void)
{
    char a[50];
    int x = 1;

    while( x > 0){
        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);
    }
}

Upvotes: 1

Views: 865

Answers (4)

Sid S
Sid S

Reputation: 6125

Don't change anything you don't want to change.

Replace :

a[i]=a[i]^32;

with

if (a[i] >= 'A' && a[i] <= 'Z' ||
    a[i] >= 'a' && a[i] <= 'z')
{
    a[i] = a[i] ^ 32;
}

If you #include <ctype.h> you can do it like this:

a[i] = islower(a[i]) ? toupper(a[i])
                     : tolower(a[i]);

Upvotes: 2

chqrlie
chqrlie

Reputation: 145307

The C Standard specifies functions in <ctype.h> to handle case for the basic character set:

  • islower(c) tests if the character is lower-case
  • isupper(c) tests if the character is upper-case
  • tolower(c) converts any character to its lower-case version
  • toupper(c) converts any character to its upper-case version

Here is how to use these:

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

int main(void) {
    char a[50];

    if (fgets(a, sizeof a, stdin)) {
        int i;
        for (i = 0; a[i] != '\0'; i++) {
            unsigned char c = a[i];
            a[i] = islower(c) ? toupper(c) : tolower(c);
        }
        printf("%s", a);
    }
    return 0;
}

Upvotes: 1

chux
chux

Reputation: 154322

... convert lowercase and uppercase letters using symbols
... when I use a symbol like '_' or '.' or ',' .... on the input I get random character

Use isupper(), tolower(), toupper(). This is the standard and highly portable way to toggle case. Consider there are more than 26 letters on various platforms.

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

int main(void ){
  char a[50];
  {
    gets(a);   // dubious, consider fgets().
    int i,length=0;

    for(i=0;a[i];i++){
      unsigned char ch = a[i];
      if (isupper(ch) {
        a[i]= tolower(ch);
      } else {
        a[i]= toupper(ch);
      }
    }
    printf("%s",a);  // use `a` , not `&a`
  }
}

If code wants to toggle case without using standard function, and knowns the char is a letter, code could use the following. It reasonably assumes that A-Z and a-z differ by one bit as is the case in ASCII and EBCDIC

        // Avoid magic numbers like 32
        a[i] ^= 'a' ^ 'A';

Still recommend to use standard functions.

Upvotes: 3

Guga Loks
Guga Loks

Reputation: 21

You can't. See ascii table. What differs from an uppercase letter from a lowercase letter is the bit 5 00100000b, which is equivalent in decimal to 32. But this only works with letters.

You should put ifs to treat anything that is not a letter.

Upvotes: -2

Related Questions