Introduction
In this program, I am going to write a c program to display a string in capital letters only. We will read a string from the user in any case i.e. lower case or upper case or mixed case but display this string only in upper case letters.
This program can be written in many ways.
- Without using standard library functions
- Using standard library function i.e. islower() and toupper()
Now, how we are going to implement these steps, please see the following both the programs.
C program to display a string in capital letters without using standard library functions
Here, we are going to perform following steps:
- We are reading a string from the user in any case i.e. lower or upper or mixed.
- After that, we are verifying each and every character whether it is a lower case or not. If it is the lower case then we will convert that lower case into upper case, otherwise, we will write it as it is.
- For verifying and converting a lower case letter, we are going to use ASCII values.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];
char ch;
int i=0;
printf("Enter any string in any case\n");
gets(str);
printf("Given String in Capital Letters: ");
while((ch=str[i])!='\0')
{
if((ch>=97)&&(ch<=122))
{
printf("%c",ch-32);
}
else
{
printf("%c",ch);
}
i++;
}
return 0;
}
Output
Enter any string in any case
Hello, welcome to HPlus Academy
Given String in Capital Letters: HELLO, WELCOME TO HPLUS ACADEMY
In the above c program, we have used ASCII values for converting lowercase letters into uppercase letters. See the following detailed explanation of the above program.
char str[100];
This is character array which is going to store the string entered by the user.
char ch;
We are reading each and every character from the string str and store in the variable ch.
int i = 0;
We are going to read a whole string character by character. For this, while loop will be used and therefore, we require the loop variable and i is the loop variable. We have initialized the value of i is equal to 0. Why? Because the character array starts from 0.
gets(str);
This function is used to read a string from the console and store that string in the variable str.
while((ch=str[i])!=’\0′)
In this while loop, we are assigning the character str[i] to the variable ch. This loop will be terminated when the value of ch becomes null character i.e. ‘\0’ which is end of the string.
if((ch>=97)&&(ch<=122))
ASCII value of a is 97 and z is 122. In this if statement, we are verifying the character entered by the user is lower or not. That means if the ASCII value of any character is in between 97 and 122, then it is a small character or lower case character. Therefore, we need to convert it into upper case letters. How will we do it?
printf(“%c”,ch-32);
This printf statement is written inside the body of if statement. The ASCII value of A is 65 and Z is 90. When we subtract 32 from the ASCII value of any lower case letter, then we will get ASCII value of upper case letter. So, here we are doing the same.
I hope you have understood this c program to display a string in capital letters without using standard library function.
In the next section, we are writing the same program with the same output. But, there is a difference in the program code.
C program to display a string in capital letters using standard library functions
In this program, we are using two important standard library functions and they are:
- islower(char) : This function checks whether a character is lower or not.
- toupper(char): This function converts lower case letter into an upper case letter.
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int main()
{
char str[100];
char ch;
int i=0;
printf("Enter any string in any case\n");
gets(str);
printf("Given String in Capital Letters: ");
while((ch=str[i])!='\0')
{
if(islower(ch))
{
printf("%c",toupper(ch));
}
else
{
printf("%c",ch);
}
i++;
}
return 0;
}
The output of this program remains the same. Because, we have added only standard library functions and remaining part is the same. See the following explanation for some important code from the above program.
All declarations and while is same. We have included one header file here i.e. <ctype.h>. Because we are using two library functions in the above program i.e. islower() and toupper().
if(islower(ch))
In this if statement, we are simply checking whether the value of ch is in small case or not. If it is in small case, then this if condition becomes true and its body will be executed, otherwise else block will be executed.
printf(“%c”,toupper(ch));
This printf() statement is printing the upper case value of the variable ch. This will be executed only when the above if statement becomes true.
As I said above, you can write this c program to display a string in capital letters in many ways.
I hope, you like this article. If you have difficulties in these two program, please feel free to contact me.
Thank you.
Important Articles
- Interview Questions On C
- Switch Case in C Program to Calculate Area of Circle and Triangle
- C Language Program to Count the Number of Lowercase Letters in a Text File
- Program in C to Replace Capital C with Capital S in a File
- Perfect Number in C Programming using All Loops
- Reverse a Number in C
- Factorial Program in C Programming
- LCM of Two Numbers in C Programming
- GCD of Two Numbers in C Programming