# Changing Case of Strings

Written by

Pooja Rao

Converting a string into either from lowercase to uppercase or from uppercase to lowercase can be done in two ways i.e with pre-defined C functions and without them.

First let us see the simpler approach of using pre-defined C functions.

Approach 1: Using the functions strlwr() and strupr() from string.h:

• strlwr() converts the input string into lowercase and strupr() converts the string to uppercase. Both are a part of the string.h library.
• This is again a not recommended approach as strlwr and strupr are from Microsoft C library and does not work in standard C library. If you are using a Turbo C++ IDE, only then will the below approach work. The GNU library has no such function defined and will throw you an error.
• Here to scan the string we are using fgets method in order to understand how it is used, although it is not recommended.

Code:

``````#include <stdio.h>
#include<conio.h>
#include <string.h> //include the header file which contains definition of strlwr and strupr
functions.
int main()
{
clrscr();
char string ;
printf("Enter a string : ");
fgets(string, 100, stdin);     //read string input from standard input screen.
printf("The string in lower case :\n", strlwr(string));
printf("The string in upper case :\n", strupr(string));
getch();
return  0;
````}````

Output:

``````Input a string: Hello! Howdy! HII
The string in lowercase is as follows: hello! howdy! hii
````The string in uppercase is as follows: HELLO! HOWDY! HII````

Approach 2: Using the functions tolower() and toupper():

• Since, the above code is not compatible with standard C library; we have an alternate approach.
• The ctype.h library includes function tolower() – to convert string to lower case and toupper() – to convert string to uppercase.
• The ctype.h header file of the C Standard Library declares several functions that are useful for testing and mapping characters.
• The difference here is that functions included under ctype.h , work on integer values.
• Hence, while case conversion the function will consider the ASCII value of the character in order to convert it to the desired case.

Declaration:Following is the declaration for  tolower() and toupper() functions respectively.

``````int  tolower(int c);
````int  toupper(int c);````

Here c  is the letter to be converted to lowercase /uppercase; This function returns lowercase / uppercase equivalent to c, if such value exists, else c remains unchanged. The value is returned as an int value that can be implicitly casted to char.

Code:  We keep converting character at each index into lower / uppercase, continuously checking for string end in every iteration.

``````#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0, size = 100;
char *string;
printf("Input a string: ");
/* These 2 lines are very important. */
string = (char *) malloc (size);
getline (&string, &size, stdin);
while (string[i])                          //checking for null character
{
string[i] = tolower(string[i]);   //converting to lowercase
i++;
}
printf("\nThe string in lowercase is as follows: ");
puts(string);
i = 0;                                     //initializing index
while (string[i])                     //checking for null character
{
string[i] = toupper(string[i]);   //converting to lowercase
i++;
}
printf("\nThe string in uppercase is as follows: ");
puts(string);
return 0;
````}````

Output:

``````Input a string: Hello! Howdy! HII
The string in lowercase is as follows: hello! howdy! hii
````The string in uppercase is as follows: HELLO! HOWDY! HII````

Approach 3: Without using pre-defined functions of C:

• In this approach we will create two user defined functions upper and lower to convert case of characters in the string.
• Remember that strings are nothing but character arrays and have the same properties for function call and return as previously discussed while learning arrays.
• We know that inn computer memory  the ASCII codes of the characters are actually stored and understood.
• The ASCII code for A-Z varies from 65 to 91 and for a-z varies from 97 to 123.
• So in order to convert a character to lowercase we would have to add 32 i.e 97-65 = 32; the difference between both cases to the character value.
• In order to convert string’s character to upper case we subtract 32 from character.

Code:

``````#include <stdio.h>
void lower_string(char []);
void upper_string(char []);
int main()
{
int size = 100;
char *string;
printf("Enter some text of your choice: ");
string = (char *) malloc (size);
getline (&string, &size, stdin);
lower_string(string);   //function call to convert to lowercase
upper_string(string);   //function call to convert to uppercase
return 0;
}
void lower_string(char str[])
{
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z')    //checking for uppercase characters
{
str[i] = str[i] + 32;               //converting uppercase to lowercase
}
i++;
}
printf("The string in lower case: %s\n", str);
}
void upper_string(char str[])
{
int i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'a' && str[i] <= 'z')   //checking for lowercase characters
{
str[i] = str[i] - 32;              //converting to uppercase from lowercase
}
i++;
}
printf("The string in upper case: %s\n", str);
````}````

Output:

``````Enter some text of your choice: Where have YOU been ?
The string in lower case: where have you been ?
````The string in upper case: WHERE HAVE YOU BEEN ?````

Thus, we have seen various methods to perform case change operation. We also saw use of fgets and getline. Henceforth getline / getdelim will be used in all string programs. Also, key takeaway is always write a code that is compatible on all platforms. Approach 2 and 3 are examples for the same. Approach 1 is restricted to Microsoft C library.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7526784,"math_prob":0.8498525,"size":5556,"snap":"2023-40-2023-50","text_gpt3_token_len":1329,"char_repetition_ratio":0.16534582,"word_repetition_ratio":0.13943355,"special_character_ratio":0.26385888,"punctuation_ratio":0.1496726,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96345913,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-27T10:55:53Z\",\"WARC-Record-ID\":\"<urn:uuid:cb4f0913-0fdd-4f35-b9d4-c8f9f030a764>\",\"Content-Length\":\"74999\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:76e340ec-0f5f-4037-abd7-139351f79cfb>\",\"WARC-Concurrent-To\":\"<urn:uuid:9f4bcbcb-bd11-4b61-ab51-552233017ce0>\",\"WARC-IP-Address\":\"3.210.81.252\",\"WARC-Target-URI\":\"https://www.studymite.com/c-programming-language/examples/changing-case-of-strings-in-c\",\"WARC-Payload-Digest\":\"sha1:3NN22MAM5AHCLO2AXTCW4ATGAL3L7D2O\",\"WARC-Block-Digest\":\"sha1:QSOCELKIN4BA7MWOSU2KQSZUEVFHMPA6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510297.25_warc_CC-MAIN-20230927103312-20230927133312-00365.warc.gz\"}"}