Recursive function to convert uppercase to lowercase

This is a discussion on Recursive function to convert uppercase to lowercase within the C Programming forums, part of the General Programming Boards category; I am writing a functions to convert all uppercase letter in the str parameter str into lowercase. Below is my ...

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    Recursive function to convert uppercase to lowercase

    I am writing a functions to convert all uppercase letter in the str parameter str into lowercase.
    Below is my program,is there any mistake and can someone point out my wrong? thk.

    Code:
    #include<stdio.h>
    
    
    void recursiveToLower(char str[])
    
    {
      int len;
      len=strlen(str[]);      /* check length of string str */
    
      while(str[len]!='\0') {
    
      str[len]=str[len]-32;
      recursivetoLower(str[len]  );
      }
    
    }
    
    
    int main() {
    
      char str[6]="topgun";
    
      recursivetoLower(str);
    
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) There is no need to call strlen.
    2) while (str[len]!='\0') should always be false. Example: "Hello" has 5 characters. "Hello"[5] will always be the null character.
    3) Not every character set has the characters in contigious order
    4) You are not checking to see if the character is upper case before trying to convert it to lower case.

    Things I would do: Use pointers directly instead of using them to subscript an array.

    Code:
    void recursiveToLower(char *str)
    {
      while ( *str != '\0' )
      {
        if ( *str >= 'A' && *str <= 'Z' )
          *str = *str - ('A' - 'a');
        recursiveToLower(str+1);  
      }
    }
    Of course that in of itself has its own problems.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    This is only because the code is much shorter than an explanation
    Code:
    void recursiveToLower(char str[])
    {
      if ( str[0] != '\0' ) {
        str[0] = toupper( str[0] );  // do this char
        recursiveToLower( str+1 ); // recursively do the remaining chars
      }
    }
    And this is bad as well - it is NOT a \0 terminated string
    > char str[6]="topgun";
    Just say
    Code:
    char str[]="topgun";
    and let the compiler figure out the size for you
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Doh can't believe I put a while() instead of an if() in that code. I think it's time for me to go to bed now

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Me-thinks Thantos's answer locks up...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    See - this is what recursive posting does for you
    You get all these replies all mixed up with one another
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes, yes it would definatly do that.

  8. #8
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36
    thk everyone,

    anything should i need to change if using another function,

    Code:
    char* recursiveStrchr(char str[], char ch);
    this function return the point to the first occurrence of char ch in the string str, or Null if ch is not found.

    if the string 'option' ch=p, it will return 2
    if the string 'option' ch=x, it will return null

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char str[]="topgun";
    No one apparently noticed the fact that the string being passed was all lower case to begin with, so you wouldn't know if your function worked right or not anyway.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    It's a good job then that I used toupper() inside my recursiveToLower() function then (D'oh!!!)

    > char* recursiveStrchr(char str[], char ch);
    Easy enough, you've seen how to walk a string recursively, give it a go
    This isn't a free hand out show
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I noticed which is why I made comment #4

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Thantos
    I noticed which is why I made comment #4
    That really doesn't apply though, because in the time you take to check it, you could just as easily call tolower on it, saving yourself the if check alltogether. Because there really is no poin in checking, since it's getting lowered anyway. The overhead is the near the same, because you're doing a few if checks, or just calling tolower.

    So there really is no point in even checking. My point was, the entire string being passed as a test-case was already lower, so you'd never know if it worked or didn't.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually the way they had it you would notice a difference as the lower case characters would have been converted to whatever happened to lay -32 away from them on the ascii chart

    The reason I didn't use tolower() was because they weren't using tolower(). I prefer not to change their programs too much if possible.

    In their example the converted output would have been:
    "TOPGUN" (ie too upper which is not what they wanted anyways). Had they had "TOPGUN" to begin with they would have had "3/0'5." ( I bet that would have thrown them for a loop

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason I didn't use tolower() was because they weren't using tolower(). I prefer not to change their programs too much if possible.
    Ah. I was only half paying attention.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You are right that using tolower() would be better though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 04:42 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21