đ Convert Lowercase to Uppercase in C Manually (Without strupr()
)
This tutorial demonstrates how to convert all lowercase letters in a string to uppercase manually using ASCII values, without relying on the built-in strupr()
function.
We'll provide two methods:
- ✅ Using a custom function
- đ ️ Without using a function (logic in
main()
)
✅ Method 1: Using a Custom Function
#include <stdio.h>
void toUpperCase(char str[]) {
for (int i = 0; str[i] != '\0'; i++) {
// Check if lowercase letter
if (str[i] >= 'a' && str[i] <= 'z') {
// Convert to uppercase by subtracting 32
str[i] = str[i] - 32;
}
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
toUpperCase(str);
printf("Uppercase string: %s", str);
return 0;
}
đ§ Explanation:
- ASCII value for 'a' to 'z' is 97 to 122.
- ASCII value for 'A' to 'Z' is 65 to 90.
- Subtracting 32 converts a lowercase letter to uppercase.
đ ️ Method 2: Without Function (All Logic Inside main()
)
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
}
printf("Uppercase string: %s", str);
return 0;
}
đ Summary
Manual conversion of lowercase letters to uppercase helps you understand ASCII codes and character manipulation in C.
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ