Write a C program to convert lowercase to uppercase using recursion.
#include <stdio.h>
#include <string.h>
/* converts lowercase to uppercase using recursion */
void lowerToUpper(char *str) {
if (*str != '\0') {
/* lower to upper */
if (*str >= 'a' && *str <= 'z') {
*str = *str - 'a' + 'A';
}
/* recursive call */
lowerToUpper(str + 1);
}
return;
}
int main() {
char str[256];
/* get the input string from the user */
printf("Enter your input string:");
fgets(str, 256, stdin);
str[strlen(str) - 1] = '\0';
/* converts lowercase to uppercase */
lowerToUpper(str);
printf("Uppercase To Lowercase: %s\n", str);
return 0;
}
#include <string.h>
/* converts lowercase to uppercase using recursion */
void lowerToUpper(char *str) {
if (*str != '\0') {
/* lower to upper */
if (*str >= 'a' && *str <= 'z') {
*str = *str - 'a' + 'A';
}
/* recursive call */
lowerToUpper(str + 1);
}
return;
}
int main() {
char str[256];
/* get the input string from the user */
printf("Enter your input string:");
fgets(str, 256, stdin);
str[strlen(str) - 1] = '\0';
/* converts lowercase to uppercase */
lowerToUpper(str);
printf("Uppercase To Lowercase: %s\n", str);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:hello world
Uppercase To Lowercase: HELLO WORLD
Enter your input string:hello world
Uppercase To Lowercase: HELLO WORLD
No comments:
Post a Comment