| | |
C problem in strings and recursive check
![]() |
•
•
Join Date: Jan 2006
Posts: 38
Reputation:
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
Posts: 54
Reputation:
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
C Syntax (Toggle Plain Text)
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
Posts: 38
Reputation:
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]C Syntax (Toggle Plain Text)
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
yotam
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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:
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
Posts: 38
Reputation:
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
thanx
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
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 a different compiler and so i shall try and run it there
return 0;
•
•
Join Date: Feb 2006
Posts: 54
Reputation:
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;
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: input question
- Next Thread: input bianary ASCII converted into Characters
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






