Changing Case of Strings
Written by
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[100] ;
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.