Thread: problem with recursion and string

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65

    problem with recursion and string

    hi guys, i need help with this algorithm, given a string and a character, the function char *lastocc(char *s, char c) has to return a pointer to the last occurrence of the character c in the string *s, for example if the word is swimming and the char is i it shoud return ' ing ', here's what i wrote:
    Code:
    char *lastocc(char *s, char c)
    {
    	if(*s == '\0') {
    		return;
    	}
    	else if(*s != c) {
    		*lastocc(s+1, c);
    	}
    	else if(*s == c) {
    		printf("%s", s);
    		*lastocc(s+1, c);		
    	}
    	
    }
    the problem is that it returns every occurrence of the character (hence imming and ing) while it should return only the last one, i still cant figure out how to do that, any suggestion?
    Last edited by rob90; 12-09-2009 at 03:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another String Problem
    By w2look in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 12:00 PM
  2. String (?) Recursion
    By Lansan1ty in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2008, 04:43 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Problem creating a recursive string permutation function
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2006, 10:09 AM
  5. convert string into integer using recursion
    By peter_hii in forum C++ Programming
    Replies: 18
    Last Post: 08-23-2006, 10:09 AM