C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Recursive Function (On String) Posted by tokoG on 2 Jul 2006 at 11:10 PM
Hi

I need to code the programme to return the index of the given char in the string in the separated function from the main.

I coded as follows.. but it hassome bugs.
Wondered since str is a pointer array that I can't pass str[1] to the recursive function?

//PROVIDED, CAN'T CHANGE THE PARAMETER
int recursiveIndexOf(char str[], char ch) {


 //So.. I need to use recursive function to return the index of 'H' in the string. Return 0 if the first character happens to be 'H'. Return 1 if 'H' is the second element.

 if(str[0] == ch)//If the first element was'H'
   return 0;
   else {        //If not, recursive
     return 1 + recursiveIndexOf(str[1]);
   }/*end else*/


}

//THIS PART IS PROVIDED CAN'T CHANGE. The main passes the string and the character 'H to the function recursiveIndexOf
void main() {
  char* str = "ABCD,EFGHIJKL,MNOPQRSTUVW";

  printf("index = %d\n", recursiveIndexOf(str, 'H'));


  fflush(stdin);
  getchar();

}


Report
Re: Recursive Function (On String) Posted by Lundin on 2 Jul 2006 at 11:18 PM
: Hi
:
: I need to code the programme to return the index of the given char in the string in the separated function from the main.
:
: I coded as follows.. but it hassome bugs.
: Wondered since str is a pointer array that I can't pass str[1] to the recursive function?
:
:
: //PROVIDED, CAN'T CHANGE THE PARAMETER
: int recursiveIndexOf(char str[], char ch) {
: 
: 
:  //So.. I need to use recursive function to return the index of 'H' in the string. Return 0 if the first character happens to be 'H'. Return 1 if 'H' is the second element.
: 
:  if(str[0] == ch)//If the first element was'H'
:    return 0;
:    else {        //If not, recursive
:      return 1 + recursiveIndexOf(str[1]);
:    }/*end else*/
: 
: 
: }
: 
: //THIS PART IS PROVIDED CAN'T CHANGE. The main passes the string and the character 'H to the function recursiveIndexOf
: void main() {
:   char* str = "ABCD,EFGHIJKL,MNOPQRSTUVW";
: 
:   printf("index = %d\n", recursiveIndexOf(str, 'H'));
: 
: 
:   fflush(stdin);
:   getchar();
: 
: }
: 
: 

:


int recursiveIndexOf(char str[], char ch) 
{
  if(str[0] == ch)    //If the first element was'H'
    return 0;
  else if(str[0] == NULL) // need to stop the recursion at end of str
    return -1;
  else                //If not, recursive
  {
    return recursiveIndexOf(&str[1], ch);
  }/*end else*/
}

Report
Re: Recursive Function (On String) Posted by tokoG on 3 Jul 2006 at 8:46 PM
:
: int recursiveIndexOf(char str[], char ch) 
: {
:   if(str[0] == ch)    //If the first element was'H'
:     return 0;
:   else if(str[0] == NULL) // need to stop the recursion at end of str
:     return -1;
:   else                //If not, recursive
:   {
:     return recursiveIndexOf(&str[1], ch);
:   }/*end else*/
: }
: 

:

Than you Lundin

I was forgetting that I was dealing with the string with the NULL character at the end. Or if there was no match (means if the target char was not found) then stop recursive.

I was trying to manipulate the string to if it finds the comma (,) change to (;). The following worked OK only on the first comma (,) in the string but not the second or following commas.

If it hits the NULL which means the end of the string, does it have to return the whole string back (manipulated one) to the caller?

  if(str[0] == ',')
    str[0] = ';';
    else if (str[0] == NULL) {
      return -1;
    } else {
        return recursiveConvertComma(&str[1]);

Report
Re: Recursive Function (On String) Posted by Donotalo on 4 Jul 2006 at 3:59 AM
This message was edited by Donotalo at 2006-7-4 4:2:50

: :
: : int recursiveIndexOf(char str[], char ch) 
:   if(str[0] == ',')
:     str[0] = ';';
:     else if (str[0] == NULL) {
:       return -1;
:     } else {
:         return recursiveConvertComma(&str[1]);
      }
: 

:

whenever ur function found a comma, it will not call recursiveConvertComma() again because of mis used if-else if ladder. better is:
int recursiveConvertComma(char str[]) {
    if(str[0] == ',')
	str[0] = ';';
    if (str[0] == NULL)
      return -1;
    else
        return recursiveConvertComma(&str[1]);
}



~Donotalo()



Report
Re: Recursive Function (On String) Posted by Lundin on 4 Jul 2006 at 6:04 AM
: :
: : int recursiveIndexOf(char str[], char ch) 
: : {
: :   if(str[0] == ch)    //If the first element was'H'
: :     return 0;
: :   else if(str[0] == NULL) // need to stop the recursion at end of str
: :     return -1;
: :   else                //If not, recursive
: :   {
: :     return recursiveIndexOf(&str[1], ch);
: :   }/*end else*/
: : }
: : 

: :
:
: Than you Lundin
:
: I was forgetting that I was dealing with the string with the NULL character at the end. Or if there was no match (means if the target char was not found) then stop recursive.
:
: I was trying to manipulate the string to if it finds the comma (,) change to (;). The following worked OK only on the first comma (,) in the string but not the second or following commas.
:
: If it hits the NULL which means the end of the string, does it have to return the whole string back (manipulated one) to the caller?
:
:
:   if(str[0] == ',')
:     str[0] = ';';
:     else if (str[0] == NULL) {
:       return -1;
:     } else {
:         return recursiveConvertComma(&str[1]);
: 

:


Oops, it should be

else if (str[0] == '\0')

And nothing else.

Using NULL might give you compiler warnings about implicit typecast from pointer to int.
Report
Re: Recursive Function (On String) Posted by bilderbikkel on 3 Jul 2006 at 1:43 AM
'main' is of return type int, not void. See e.g. www.codepedia.com/1/CppMain for references.
bilderbikkel

Report
Re: Recursive Function (On String) Posted by Lundin on 3 Jul 2006 at 3:27 AM
: 'main' is of return type int, not void. See e.g. www.codepedia.com/1/CppMain for references.
: bilderbikkel
:
:


I have the feeling he isn't listening to you...

fflush(stdin) isn't C standard either.
http://c-faq.com/stdio/stdinflush.html
Report
Re: Recursive Function (On String) Posted by Gregry2 on 3 Jul 2006 at 6:05 AM
: : 'main' is of return type int, not void. See e.g. www.codepedia.com/1/CppMain for references.
: : bilderbikkel
: :
: :
:
:
: I have the feeling he isn't listening to you...
:
: fflush(stdin) isn't C standard either.
: http://c-faq.com/stdio/stdinflush.html
:

It was provided that the main couldn't be changed. Apparently her instructors make main void to state something to the other programmers,I guess, that main doesn't return anything special.

It isn't standard, and she just needs to noe that, but for now, she needs to pass her class, so just understand.
{2}rIng
Report
Re: Recursive Function (On String) Posted by Lundin on 3 Jul 2006 at 7:26 AM
: : : 'main' is of return type int, not void. See e.g. www.codepedia.com/1/CppMain for references.
: : : bilderbikkel
: : :
: : :
: :
: :
: : I have the feeling he isn't listening to you...
: :
: : fflush(stdin) isn't C standard either.
: : http://c-faq.com/stdio/stdinflush.html
: :
:
: It was provided that the main couldn't be changed. Apparently her instructors make main void to state something to the other programmers,I guess, that main doesn't return anything special.
:
: It isn't standard, and she just needs to noe that, but for now, she needs to pass her class, so just understand.
: {2}rIng
:


But if anyone post code here and want someone to run & test it in search for errors, they should follow ANSI C. Otherwise people will be less eager to help, since they have to convert the code before running it. Same goes for code without code tags, proper indention, code with line numbers added to it etc etc.


Report
Re: Recursive Function (On String) Posted by tokoG on 3 Jul 2006 at 8:59 PM
: : : : 'main' is of return type int, not void. See e.g. www.codepedia.com/1/CppMain for references.
: : : : bilderbikkel
: : : :
: : : :
: : :
: : :
: : : I have the feeling he isn't listening to you...
: : :
: : : fflush(stdin) isn't C standard either.
: : : http://c-faq.com/stdio/stdinflush.html
: : :


Yes... that's just what's provided from my course and I discussed with my tutor about it once regarding using the void instead of int, and using fflush(stdin).

The reason why they keep using void is to keep the coding simple. Well this is what they said and if using void is not proper I think they should start using int but what I think is when the course started, as everyone goes thru, it started from "Hello World" and because it used only printf it used void on main.

As for fflush(stdin) I also tested couple times and found it's unnecessarry unless you use scanf or other functions that needs the input buffer flush. If I just want to pause the screen, getchar(); is enough. Dont recall what they exacly explain to me... but the reason why they use is to clear any unuse buffer before pausing the screen. But I think if the code is simple like mine then we clealy know if we need or not. Then, I think everyone has their style of programming. Somehow my tutor thought to keep using it..

If I were to programme from the scratch. I use int for main function and use fflush(stin) only when I need it. For every programme so far I coded (except the one to interact with the files) I pause the screen all the time and I use only getchar();.
Report
Re: Recursive Function (On String) Posted by Eric Tetz on 4 Jul 2006 at 11:52 AM
: Yes... that's just what's provided from my course

Yeah, it's amazing how many instructors don't know the details of the language they're teaching. But really, the general programming concepts you're learning are more important than the nitty gritty details of one specific language. Let's hope your instructor is better at teaching programming than he is a teaching C.

: If I were to programme from the scratch. I use int for main function
: and use fflush(stin) only when I need it.

fflush(stdin) is not C. There's no telling what it does. It could do what you expect, or it could reformat your harddrive, the language standard doesn't say. It's undefined.

You say you tested it, but all that tells you is what it does on one particular compiler. Since that is not part of the language, your test tells you nothing about what will happen if somebody builds that code with a different compiler.

As for main returning int, that's what the standard says it must return. Some older compilers, or compilers that are trying to stay backwards compatible with old code, will accept 'void main', but many will not; it's not standard C.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.