Introduction
In this article, we will explore two different approaches to create a C program to display a string in capital letters only.
The first approach will utilize library functions, while the second approach will implement the conversion without using any library functions.
By understanding both methods, you can choose the one that best suits your requirements.
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
Let’s dive into the details and learn how to implement these programs step by step.
Understanding the Problem
Before we delve into the programs, let’s understand the problem at hand. We need to create C programs that take a string as input and display the string with all its characters converted to capital letters.
For example, if the input string is “Hello, World!”, the programs should display “HELLO, WORLD!” as the output.
The programs should preserve the positions of all non-alphabetic characters, such as punctuation marks or spaces.
Also Read: 25 Interview Questions On C: A Comprehensive Guide for Success
Approach 1: Using Library Functions
The first approach involves utilizing library functions to convert the characters to uppercase.
The C standard library provides the toupper()
function, which converts a lowercase character to its uppercase equivalent.
By iterating over each character of the string and applying the toupper()
function, we can achieve the desired result.
Also Read: Unveiling Looping Constructs: Exploring the Do-While Loop
Code Implementation
#include <stdio.h>
#include <ctype.h>
void displayInCapitalLetters(char *str) {
int i = 0;
char c;
while (str[i] != '\0') {
c = str[i];
if (isalpha(c)) {
c = toupper(c);
}
printf("%c", c);
i++;
}
}
int main() {
char str[] = "Hello, World!";
printf("Original String: %s\n", str);
printf("Capital Letters Only: ");
displayInCapitalLetters(str);
return 0;
}
Also Read: Boost Python Code Efficiency: Eliminating Loops for Enhanced Performance
Approach 2: Without Using Library Functions
The second approach involves manually converting the characters to uppercase without relying on library functions.
This approach requires a bit more coding, but it avoids using external functions, which can be beneficial in certain scenarios where library functions are restricted.
Code Implementation
#include <stdio.h>
void displayInCapitalLetters(char *str) {
int i = 0;
char c;
while (str[i] != '\0') {
c = str[i];
if (c >= 'a' && c <= 'z') {
c = c - 32;
}
printf("%c", c);
i++;
}
}
int main() {
char str[] = "Hello, World!";
printf("Original String: %s\n", str);
printf("Capital Letters Only: ");
displayInCapitalLetters(str);
return 0;
}
Also Read: C Program to Find the Sum of Cubes of Elements in an Array
Example Usage
Let’s run the programs with the given input string “Hello, World!” to see the output:
Original String: Hello, World!
Capital Letters Only: HELLO, WORLD!
As we can see, both programs successfully convert the input string to capital letters only, preserving the positions of non-alphabetic characters.
FAQs
Yes, both programs can handle strings with special characters or numbers. They only modify alphabetic characters and leave other characters unchanged.
If the input string is empty (i.e., contains no characters), both programs will display nothing since there are no characters to convert.
There are no inherent limitations to the length of the input string in these programs. However, you should ensure that the string fits within the available memory.
To modify the programs to accept user input, you can use the scanf()
function to read a string from the user instead of hard-coding it in the main()
function.
Yes, both programs can convert lowercase letters to uppercase. The toupper()
function in the first program and the manual conversion in the second program handle the conversion.
In terms of performance, the program using library functions (toupper()
) may have a slight advantage due to potential optimizations in the library implementation. However, the performance difference is usually negligible unless you are working with extremely large strings or have specific optimization requirements.
Conclusion
In this article, we explored two different approaches to creating C program to display a a string in capital letters only.
The first approach utilized the toupper()
function from the C standard library, while the second approach manually converted the characters without using any library functions.
Both programs achieved the desired result, and you can choose the approach based on your specific requirements.
Now you have the knowledge to implement C programs that convert strings to capital letters, giving you greater control over string manipulation.