C Programming: Replace every lowercase letter with the same uppercase
C String: Exercise-40 with Solution
Write a C program to replace each lowercase letter with the same uppercase letter of a given string. Return the newly created string.
Sample Data:
("Python") -> "PYTHON"
("abcdcsd") -> "ABCDCSD"
Sample Solution-1:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to replace lowercase letters with their uppercase counterparts
char * test(char * text) {
// Check if the string is empty
if (!text[0])
return "No string found.";
char *tt = text;
// Loop through the string
while(*tt != '\0') {
// If the current character is a lowercase letter, convert it to uppercase
if (*tt >= 'a' && *tt <= 'z') {
*tt = *tt + 'A' - 'a'; // Conversion to uppercase
}
tt++;
}
return text;
}
int main() {
// Example strings for testing
//char text[50] = "";
char text[50] = "Python";
//char text[50] = "abcdcsd";
printf("Original string: %s", text);
printf("\nReplace each lowercase letter with the same uppercase in the said string:\n%s ", test(text));
return 0;
}
Sample Output:
Original string: Python Replace each lowercase letter with the same uppercase in the said string: PYTHON
Flowchart :
Sample Solution-2:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to replace lowercase letters with their uppercase counterparts
char * test(char * text) {
// Check if the string is empty
if (!text[0])
return "No string found.";
int i, str_len = strlen(text);
// Allocate memory for the resulting string with the same length as the input
char * result_str = (char *) malloc(sizeof(char) * str_len);
// Loop through each character in the string
for (i = 0; i < str_len; i++) {
// If the character is a lowercase letter, convert it to uppercase
if (*(text + i) >= 'a' && *(text + i) <= 'z') {
*(result_str + i) = *(text + i) - 32; // Conversion to uppercase
} else {
*(result_str + i) = *(text + i); // Keep the character unchanged if it's not a lowercase letter
}
}
*(result_str + str_len) = '\0'; // Add the null terminator to mark the end of the string
return result_str;
}
int main() {
// Example strings for testing
//char text[50] = "";
//char text[50] = "Python";
char text[50] = "abcdcsd";
printf("Original string: %s", text);
printf("\nReplace each lowercase letter with the same uppercase in the said string:\n%s ", test(text));
return 0;
}
Sample Output:
Original string: abcdcsd Replace each lowercase letter with the same uppercase in the said string: ABCDCSD
Flowchart :
C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous C Programming Exercise: Longest Palindromic Substring from a given string.
Next C Programming Exercise: Length of longest common subsequence of two strings
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/string/c-string-exercise-40.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics