Recursion

This is a discussion on Recursion within the C Programming forums, part of the General Programming Boards category; This program suppose to count the string length that are A-Z,a-z, and 0-9 using recursion but it counts everything, why?? ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    27

    Recursion

    This program suppose to count the string length that are A-Z,a-z, and 0-9 using recursion but it counts everything, why??

    Code:
    #include <stdio.h>
    
    int count(char string[])
    {
        int i = 0;
    
        if(string[i] == 0)
        {
            return 0;
        }
    
        else if((string[i]>=48)||(string[i]<=57)||(string[i]>=65)||(string[i]<=90)||(string[i]>=97)||(string[i]<=122))
        {
            printf("%d\n",string[i]);
            return (1 + count(&string[i+1]));
    
        }
    
        else
        {
            return 0;
        }
    }
    
    int main()
    {
        char buffer[64];
        char string[64];
        int i = 0;
        int len = 0;
    
        printf("Enter string: ");
        fgets(buffer,63,stdin);
        sscanf(buffer,"%s",string);
    
        len = count(&string[i]);
        printf("Length: %d\n",len);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Check the condition of your recursive step; it is not quite right.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    ((string[i]>=48)||(string[i]<=57)||(string[i]>=65)||(string[i]<=90)||(string[i]>=97)||(string[i]<=122))

    probably need some &&s in there, cause right now any ASCII character will return true for that statement...

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  4. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:29 PM

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