Thread: Quick hint on assignment (Recursion in C- Beginner)

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    5

    Quick hint on assignment (Recursion in C- Beginner)

    Trying to a quick program that tells how many instances of a certain character there are in a word. It seems like I'm on the right track, but with some hitches.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    unsigned howMany(char c, const const * s);
    
    int main(){
    
    char *s = "Mississippi";
    char c = 'o';
        printf_s("There are %u '%c's", howMany(c, s), c);
    
    unsigned howMany(char c, const const *s) {
        if (*s == '\0') return 0;
        else if (*s != c) { s++; howMany(c, s); }
        else (*s == c); {
            s++;
            return 1 + howMany(c, s);
        }
    }

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Do you *need* to use recursive approach here? It's a really forced, bad example of recursion. (thus I assume it's an assignment)

    About the code - take the howMany function definition out of main function body. Or did you forget about a closing curly bracket at line 12?
    Look at your howMany function - in case of the second branch (*s != c) you don't return a value from the function.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There's also an extraneous semicolon on line 16. That effectively creates and empty body for the else, and the code on lines 17-18 is always executed.

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Yeah, unfortunately it's an assignment where we have to use recursion instead of any loops. And yikes, the missing curly bracket was some sloppy copy and pasting on my part.

    Does the [code]
    if (*s!=c) [\code] have to return a value? I was under the impression that as long as we had a command afterwards, it was fine. I'm thinking I may less knowledgeable than I thought I was...

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Question about the semicolon: I'm using Visual Studio 2015 and for some reason they insisted on the semicolon. I thought that that was improper syntax as well, but the program would only run with that semicolon. Is that a design specific to VS?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Else does not need a condition. If you want to write one anyway for clarity's sake, then make it a comment. The compiler just thinks that's what you want to do on the else path.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Duh, I totally missed another obvious point. You can't have a condition on an else unless it's an else if. But you probably don't need an else if, just an else.

  8. #8
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You promised the compiler that the function will return an unsigned int value, but in case the second branch is executed you let the execution to reach end of a function body without returning a value - which is only valid if a function returns void. Don't break your promises like that - I'm sure your compiler is very, very sad... :P

  9. #9
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Haha, thanks! That makes sense.

  10. #10
    Registered User
    Join Date
    Nov 2016
    Posts
    5
    Aaaaah, got it! Thanks, guys.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Also like to add - if your learning C, it's best to not use visual studio. C89 is the only standard of the language
    the C compiler knows, so any newer features from C99 and C11 will be unavailable to use. Best to use a more
    standard compliant compiler such as GCC and code::blocks.
    Double Helix STL

  12. #12
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Is clang supported in Visual Studio already? If so - that would also be a good idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner. Help with a class assignment. (Function)
    By SkywardTaco in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2015, 03:03 PM
  2. Quick question (beginner)
    By JMaxwell in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2013, 02:49 PM
  3. Recursion and Quick Sort
    By black_stallion in forum C Programming
    Replies: 5
    Last Post: 10-29-2011, 03:54 PM
  4. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  5. GCC Beginner - Quick Question
    By samGwilliam in forum C Programming
    Replies: 5
    Last Post: 02-25-2006, 11:58 AM

Tags for this Thread