Write a C program to print uppercase characters in a string using recursion.
#include <stdio.h>
#include <string.h>
/* print uppercase character in give character using recursion */
void printUpper(char *str) {
if (*str != '\0') {
/* prints uppercase characters alone */
if (*str >= 'A' && *str <= 'Z') {
printf("%c", *str);
}
printUpper(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';
/* prints uppercase character in the given input */
printf("Upper case characters in given string:");
printUpper(str);
printf("\n");
return 0;
}
#include <string.h>
/* print uppercase character in give character using recursion */
void printUpper(char *str) {
if (*str != '\0') {
/* prints uppercase characters alone */
if (*str >= 'A' && *str <= 'Z') {
printf("%c", *str);
}
printUpper(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';
/* prints uppercase character in the given input */
printf("Upper case characters in given string:");
printUpper(str);
printf("\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:HeLlO WoRlD
Upper case characters in given string:HLOWRD
Enter your input string:HeLlO WoRlD
Upper case characters in given string:HLOWRD
No comments:
Post a Comment