English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

str = "Hello world"
ch = "w"
use recursive method, no string.h function
return 'w' index = 6

int findIndexString(char str[ ], char ch)

2006-07-03 19:56:53 · 3 answers · asked by Kaka 1 in Computers & Internet Programming & Design

3 answers

again, like before! I really hv too much time in my hands today :-p

#include

int findIndexString(char str[], char ch) {
int pos;
if (*str=='\0') {
return -1;
}
if(*str==ch) {
return 0;
}
pos = findIndexString(++str, ch);
if (pos<0) {
return pos;
} else {
return pos+1;
}
}
int main() {
char str[25]="hello world";
int pos = findIndexString(str, 'h');
printf("The position is %d\n", pos);
}

2006-07-03 20:14:19 · answer #1 · answered by Neil 5 · 0 0

int findIndexString(char str[],char ch)
{
static int i=0;
if(*str =='\0')
return 0;
else if(*str == ch)
return i;
else{
i++;
return findIndexString(++str,ch);
}

}

2006-07-04 05:14:30 · answer #2 · answered by Tom 2 · 0 0

Hello,

I suppose this is just for fun? If so, ok.

Else please note It is particularly inefficient to use recursiveness for such low level functions. You put the system under heavy strain due to call stack adjustments needed to accomodate the numerous function calls (passing variables).

2006-07-04 04:08:49 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers