This blog is under construction

Sunday 21 July 2013

C program to convert uppercase to lowercase using recursion

Write a C program to convert uppercase to lowercase using recursion.


  #include <stdio.h>
  #include <string.h>

  /* converts uppercase to lowercase using recursion */
  void upperToLower(char *str) {
        if (*str != '\0') {
                /* upper to lower */
                if (*str >= 'A' && *str <= 'Z') {
                        *str = *str - 'A' + 'a';
                }

                /* recursive call */
                upperToLower(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 uppercase to lowercase */
        upperToLower(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


No comments:

Post a Comment