Andrei Cusnir
Andrei Cusnir

Reputation: 2805

Convert lowercase to uppercase using ASCII

I am trying to convert all lowercase to uppercase letter, using the ASCII table! It is very easy to deal and i have figured out the code. Problem is, that if there is a space between the words, then the program will only change the first word and after the space it will not print anything.

Example
Word: Andreas Gives: ANDREAS
Word: TeSt123Ha Gives: TEST123HA
BUT!!!
Word: Hello 45 Gives: HELLO
after the space it prints nothing!

I know that the space in ASCII table is equal to 32, and in my code i tell the program that if the current code that you are reading is not between 97 and 122, then don't perform any changes!

But it is still not working!

char currentletter;
int i;

for (i=0; i<49; i++)    
{
    currentletter = str[i];

    if ((currentletter > 96) && (currentletter < 123))
    {
        char newletter;
        newletter = currentletter - 32;
        str[i] = newletter;
    }
    else
    {
        str[i] = currentletter;
    }
}
printf("%s\n", str);

Upvotes: 0

Views: 67546

Answers (3)

Spikatrix
Spikatrix

Reputation: 20244

You have mentioned in one of the comments that you use scanf("%s", str); to get the string. The problem is that %s will stop scanning once it finds a whitespace character. In your case, it stops scanning when it sees the space character.

Use fgets() if you want to scan one whole line:

fgets(str, sizeof(str), stdin);

Once thing to note here is that fgets will scan in the newline character into the string as well.


Your code can be simplified to:

for (int i = 0; str[i] != '\0'; i++) // Loop until the NUL-terminator
{
    if ((str[i] >= 'a') && (str[i] <= 'z')) // If the current character is a lowercase alphabet
        str[i] = str[i] - ('a' - 'A');      // See the ASCII table to understand this:
                                            // http://www.asciitable.com/index/asciifull.gif
}

printf("%s\n", str);

Or a more easier way would be to use tolower from ctype.h:

#include <ctype.h>

for(int i = 0; str[i] != '\0'; i++) // Loop until the NUL-terminator
{
    str[i] = tolower(str[i]); // Convert each character to lowercase (It does nothing if the character is not an alphabet)
}

printf("%s\n", str);

Upvotes: 3

Cozmo
Cozmo

Reputation: 53

I gave it a try using STL and a Lambda just for fun:

string input = "";
getline(cin, input);
transform(input.begin(), input.end(), input.begin(), [](char c) { return (c > 96 && c < 123) ? c ^= 0x20 : c; });
copy(input.begin(), input.end(), ostream_iterator<char>(cout, " "));

I compiled and tested with c++ 17 in Visual Studio 2019, and did not perform exhaustive testing!

Upvotes: -1

Guest0x20
Guest0x20

Reputation: 146

flipping the 5th lowest bit should help.

Each lowercase letter is 32 + uppercase equivalent. This means simply flipping the bit at position 5 (counting from least significant bit at position 0) inverts the case of a letter. https://web.stanford.edu/class/cs107/lab1/practice.html

char *str;
int str_size = sizeof(str);

for(int i=0; i<str_size;i++){
   if((str[i]>96) && (str[i]<123)) str[i] ^=0x20;
} 

Upvotes: 13

Related Questions