Thread: string question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    string question..

    i got two helping functions:


    *FindFirst(char *str,char c) char

    returns the pointer to a place in the given string when char c appears for the 1st time.


    char *FindLast(char* str,char c, int n)

    returns the pointer to a place in the given string when char c appears for the n ' th time.

    i need to build a recursive only function
    char*MinSubstring( char *str, int n, char c, int *len)

    which returns a pointer to a place where a substring with the minimal length starts ,in
    which char c appears for the n times.
    parameter len should store the length of our found string.

    i cant think of an algorithm to do that??

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    ...
    i cant think of an algorithm to do that??
    Note that the minimal length substring will start and end with the character c.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of thinking "What algorithm will do this?", I believe you should work the problem out by hand several times, and then you'll have learned an algorithm to do this problem.

    The purpose is to make you sit down and figure out the problem by hand, and then put that solution into pseudo-code, (or a flow chart), and then translate that into code, function by function.

    Asking for help before you even have learned how to do the problem by hand, just defeats the purpose of your learning, which is to learn how to solve these problems.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant think of a general way

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm pretty sure you haven't tried ANYTHING here. Take the first example. Show us your code how you'd search for a character in a string, and return the place where it is. Go.

    Without any effort, we're not so inclined to "help" you.


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

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by transgalactic2 View Post
    i cant think of a general way
    I can't think that you're trying when you show no evidence of working on this problem, at all.

    You're not a raw beginner, anymore.

    Get to work: first with pencil and paper, solving it by hand, then with pseudo code or flow charts, and then by building up the code, function by function.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am thinking of taking the first place of the string
    and making FindLast(char* str,char c, int n)
    then i substract the first place with the place which were returned and save the subtraction

    then i compare the length with the length i will get from
    MinSubstring(str+1, n, c,&len);

    i save in len the minimal length

    and return the place where the minimal string starts.

    is there any problem in this code

    Code:
    char*MinSubstring( char *str, int n, char c, int *len)
    {
    	int length,next_length;
    	
        char* place,next_place;
    	
    	place=FindLast(str,c,n);
    	length=place-str;
    	if (*str!='\0')
    	next_place=MinSubstring(str+1,n,c,&next_length);
        if (next_length>length)
    	{
          *len=length;
    	  return place;
    	}
    	else
    	{
           *len=next_length;
    	  return next_place;
    	}
    }

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to do flow chart on this problem?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    is there any problem in this code
    You mean other than the fact that next_length and next_place will be uninitialized if *str is in fact 0, and the fact that next_place is a char rather than a char*?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how * str will be 0??
    Code:
    char*MinSubstring( char *str, int n, char c, int *len)
    {
    	int length,next_length;
    	
        char* place;
    	char *next_place
    	place=FindLast(str,c,n);
    	length=place-str;
    	if (*str!='\0')
    	next_place=MinSubstring(str+1,n,c,&next_length);
        if (next_length>length)
    	{
          *len=length;
    	  return place;
    	}
    	else
    	{
           *len=next_length;
    	  return next_place;
    	}
    }
    i fixed what you said,any more logical problems?

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well how does it behave when you run it? You should write a program that calls your function with a test string and displays the results. See what this function does, and then see what needs to change to fix any problems.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's your code indented so that I can read it. (Oh, and with a semicolon added so that it would have a better chance of compiling.)
    Code:
    char *MinSubstring( char *str, int n, char c, int *len)
    {
        int length, next_length;
        
        char* place;
        char *next_place ;
        place=FindLast(str,c,n);
        length=place-str;
        
        if (*str!='\0')
            next_place=MinSubstring(str+1,n,c,&next_length);
        
        if (next_length>length)
        {
            *len=length;
            return place;
        }
        else
        {
            *len=next_length;
            return next_place;
        }
    }
    You say if (*str!='\0'), and then if that's the case you go on to set next_place and (presumably) next_length. But what if this if doesn't execute? i.e., what if *str == '\0'? (That's what I meant when I said "if *str is in fact 0". '\0' and 0 are the same thing.)

    What will happen is that next_place and next_length won't have been assigned any values at all. They'll be uninitialized. But then you go and execute this:
    Code:
        if (next_length>length)
    Do you think that's going to work if next_length hasn't been assigned a value? Hint: not very well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    char *MinSubstring( char *str, int n, char c, int *len)
    {
        int length, next_length;
        
        char* place;
        char *next_place ;
        place=FindLast(str,c,n);
        length=place-str;
        
        if (*str=='\0')
           *next_length=9999999;
            return NULL;
        if (*str!='\0')
            next_place=MinSubstring(str+1,n,c,&next_length);
        
        if (next_length>length)
        {
            *len=length;
            return place;
        }
        else
        {
            *len=next_length;
            return next_place;
        }
    }
    now it solves the problem ?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Umm, no. Since you haven't put in curly braces, the "return NULL" always executes . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM