Please support our C advertiser: Programming Forums
Views: 1241 | Replies: 9
![]() |
•
•
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation:
Rep Power: 3
Solved Threads: 0
Hello to you all ,
I have been assigned to in C to build a function which will take 2 strings and count (in recursive way) how many chars are alike (case sensitive).
I have setup the main and also a function which recieves the input from user .
I have a problem converting the theory to commands :
if the function recieves 2 strings , the comparison needs to letter to letter with the same index , and adding 1 to the returned value if they are equal, in which the advance down the recursive process is "index+1".
I tried it , but it doesnt work and i have no idea howcome .
Code :
int func(char s1[], char s2[])
{
int i=0;
if (s1[i]!=s2[i]) return 0;
if (s1[i]==s2[i]) return 1+func(s1[i+1],s2[i+1]);
}
am i missing something ?
thank you
Yotam
I have been assigned to in C to build a function which will take 2 strings and count (in recursive way) how many chars are alike (case sensitive).
I have setup the main and also a function which recieves the input from user .
I have a problem converting the theory to commands :
if the function recieves 2 strings , the comparison needs to letter to letter with the same index , and adding 1 to the returned value if they are equal, in which the advance down the recursive process is "index+1".
I tried it , but it doesnt work and i have no idea howcome .
Code :
int func(char s1[], char s2[])
{
int i=0;
if (s1[i]!=s2[i]) return 0;
if (s1[i]==s2[i]) return 1+func(s1[i+1],s2[i+1]);
}
am i missing something ?
thank you
Yotam
•
•
Join Date: Feb 2006
Location: India
Posts: 53
Reputation:
Rep Power: 3
Solved Threads: 2
[quote=YoTaMiX]Code :
/QUOTE]
>>if (s1[i]!=s2[i]) return 0;
here think what happens if the first character does not match
does it remain the type of function you want..
and you need sum kind of parameter checking to end the recursive loop
add the condition of some kind
sanket
int func(char s1[], char s2[])
{
int i=0;
if (s1[i]!=s2[i]) return 0;
if (s1[i]==s2[i]) return 1+func(s1[i+1],s2[i+1]);
}>>if (s1[i]!=s2[i]) return 0;
here think what happens if the first character does not match
does it remain the type of function you want..
and you need sum kind of parameter checking to end the recursive loop
add the condition of some kind
sanket
return 0;
•
•
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation:
Rep Power: 3
Solved Threads: 0
[quote]
the first IF is the stop condition for loop , if first letter doesnt match , it will not run on the rest of the strings. no?
yotam
•
•
•
•
Originally Posted by HackWizz
••••Originally Posted by YoTaMiXCode :
/QUOTE]int func(char s1[], char s2[]) { int i=0; if (s1[i]!=s2[i]) return 0; if (s1[i]==s2[i]) return 1+func(s1[i+1],s2[i+1]); }
>>if (s1[i]!=s2[i]) return 0;
here think what happens if the first character does not match
does it remain the type of function you want..
and you need sum kind of parameter checking to end the recursive loop
add the condition of some kind
sanket
the first IF is the stop condition for loop , if first letter doesnt match , it will not run on the rest of the strings. no?
yotam
•
•
Join Date: Jul 2005
Posts: 1,344
Reputation:
Rep Power: 9
Solved Threads: 182
In each call to the function i starts at 0, given the declaration int i = 0; Therefore you never check beyond the first elements of the array no matter how many times the function calls itself.
The function will stop calling if the first elements of each string are not the same.
The function will call itself if the first element of each string are the same, but it will call itself using the wrong parameters. The function definition says that it should be passed to char arrays, but the recursive call sends it two characters.
Somehow I don't think any of that is what you intended. My first piece of advice would be to comment your code so you can compare what you intend to do in English with what you do in code.
The function will stop calling if the first elements of each string are not the same.
The function will call itself if the first element of each string are the same, but it will call itself using the wrong parameters. The function definition says that it should be passed to char arrays, but the recursive call sends it two characters.
Somehow I don't think any of that is what you intended. My first piece of advice would be to comment your code so you can compare what you intend to do in English with what you do in code.
•
•
Join Date: Mar 2006
Posts: 38
Reputation:
Rep Power: 3
Solved Threads: 1
its foolish that in the function every time u r initializing i with 0 and returning home if null or not moving further because u still are with i=0 in the remaining iterations also.
solution to ur code is declare ur int as static int.
then the problem is solved
get help at >>> prof.thakur@yahoo.co.in
solution to ur code is declare ur int as static int.
then the problem is solved
get help at >>> prof.thakur@yahoo.co.in
•
•
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation:
Rep Power: 3
Solved Threads: 0
•
•
•
•
Originally Posted by prof.thakur
its foolish that in the function every time u r initializing i with 0 and returning home if null or not moving further because u still are with i=0 in the remaining iterations also.
solution to ur code is declare ur int as static int.
then the problem is solved
get help at >>> prof.thakur@yahoo.co.in
I am putting up the code in attachment.... i tried watching the process and i still cant find whats wrong
thanx
•
•
Join Date: Feb 2006
Location: India
Posts: 53
Reputation:
Rep Power: 3
Solved Threads: 2
•
•
•
•
Originally Posted by YoTaMiX
I am putting up the code in attachment.... i tried watching the process and i still cant find whats wrong
thanx
I have not compiled and ran the code...but see that when you increment s1 and s2 use ++s1 so that it moves the pointer and then passes the arguement.
I have a different compiler and so i shall try and run it there
return 0;
•
•
Join Date: Feb 2006
Location: India
Posts: 53
Reputation:
Rep Power: 3
Solved Threads: 2
•
•
•
•
Originally Posted by YoTaMiX
I am putting up the code in attachment.... i tried watching the process and i still cant find whats wrong
thanx
but some changes are required..well when u compare s1 and s2 the char is not compared so change to *s1 and *s2..also after this the program calculates the first consecutive same chars and including spaces..otherwise the code is ok
return 0;
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode