Recursive Function (On String) - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Recursive Function (On String)

Posts: 209Member
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 [b]str[/b] is a [b]pointer array[/b] that I can't pass [b]str[1][/b] to the recursive function?

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

//[red]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*/

[/red]
}

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

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

fflush(stdin);
getchar();

}
[/blue]
[/code]

• Posts: 3,711Member
: 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 [b]str[/b] is a [b]pointer array[/b] that I can't pass [b]str[1][/b] to the recursive function?
:
: [code]
: //[blue]PROVIDED, CAN'T CHANGE THE PARAMETER
: int recursiveIndexOf(char str[], char ch) {
: [/blue]
:
: //[red]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*/
:
: [/red]
: }
:
: //[blue][b]THIS PART IS PROVIDED CAN'T CHANGE. The main passes the string and the character 'H to the function [b]recursiveIndexOf[/b]
: void main() {
: char* str = "ABCD,EFGHIJKL,MNOPQRSTUVW";
:
: printf("index = %d
", recursiveIndexOf(str, 'H'));
:
:
: fflush(stdin);
: getchar();
:
: }
: [/blue]
: [/code]
:

[code]
int recursiveIndexOf(char str[], char ch)
{
if(str[0] == ch) //If the first element was'H'
return 0;[red]
else if(str[0] == NULL) // need to stop the recursion at end of str
return -1;[/red]
else //If not, recursive
{
return recursiveIndexOf([red][b]&[/b][/red]str[1][red], ch[/red]);
}/*end else*/
}
[/code]
• Posts: 754Member
'main' is of return type int, not void. See e.g. www.codepedia.com/1/CppMain for references.
bilderbikkel

• Posts: 3,711Member
: '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
• Posts: 607Member
: : '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
• Posts: 3,711Member
: : : '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.

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

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?

[code]
if(str[0] == ',')
str[0] = ';';
else if (str[0] == NULL) {
return -1;
} else {
return recursiveConvertComma(&str[1]);
[/code]
• Posts: 209Member
: : : : '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 [b]void[/b] instead of [b]int[/b], and using [b]fflush(stdin)[/b].

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

As for [b]fflush(stdin)[/b] I also tested couple times and found it's unnecessarry unless you use [b]scanf[/b] or other functions that needs the input buffer flush. If I just want to [b]pause the screen[/b], [b]getchar();[/b] 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 [b]int[/b] for main function and use [b]fflush(stin)[/b] 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 [b]getchar();[/b].
• Posts: 715Member
[b][red]This message was edited by Donotalo at 2006-7-4 4:2:50[/red][/b][hr]
: : [code]
: : int recursiveIndexOf(char str[], char ch)
: if(str[0] == ',')
: str[0] = ';';
: [red]else if (str[0] == NULL) {[/red]
: return -1;
:[red] } else {[/red]
: return recursiveConvertComma(&str[1]);
}
: [/code]
:
[purple]
whenever ur function found a comma, it will not call [blue]recursiveConvertComma()[/blue] again because of mis used if-else if ladder. better is:[code]
int recursiveConvertComma(char str[]) {
if(str[0] == ',')
str[0] = ';';
if (str[0] == NULL)
return -1;
else
return recursiveConvertComma(&str[1]);
}
[/code]
[/purple]
[hr][purple]~Donotalo()[/purple]

• Posts: 3,711Member
: : [code]
: : int recursiveIndexOf(char str[], char ch)
: : {
: : if(str[0] == ch) //If the first element was'H'
: : return 0;[red]
: : else if(str[0] == NULL) // need to stop the recursion at end of str
: : return -1;[/red]
: : else //If not, recursive
: : {
: : return recursiveIndexOf([red][b]&[/b][/red]str[1][red], ch[/red]);
: : }/*end else*/
: : }
: : [/code]
: :
:
: 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?
:
: [code]
: if(str[0] == ',')
: str[0] = ';';
: else if (str[0] == NULL) {
: return -1;
: } else {
: return recursiveConvertComma(&str[1]);
: [/code]
:

Oops, it should be

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

And nothing else.

Using NULL might give you compiler warnings about implicit typecast from pointer to int.
• Posts: 2,141Member
: 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 [b]int[/b] for main function
: and use [b]fflush(stin)[/b] 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 [italic]one particular compiler[/italic]. 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 [italic]must[/italic] 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.