RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1241 | Replies: 9
Reply
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation: YoTaMiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Help C problem in strings and recursive check

  #1  
Apr 2nd, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Location: India
Posts: 53
Reputation: HackWizz is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Help Re: C problem in strings and recursive check

  #2  
Apr 2nd, 2006
[quote=YoTaMiX]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]);
}
/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
return 0;
Reply With Quote  
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation: YoTaMiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Re: C problem in strings and recursive check

  #3  
Apr 2nd, 2006
[quote]
Originally Posted by HackWizz
Originally Posted by YoTaMiX
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]);
}
/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

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
Reply With Quote  
Join Date: Jul 2005
Posts: 1,344
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 182
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: C problem in strings and recursive check

  #4  
Apr 2nd, 2006
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.
Reply With Quote  
Join Date: Feb 2006
Location: India
Posts: 53
Reputation: HackWizz is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: C problem in strings and recursive check

  #5  
Apr 2nd, 2006
hey yotamix,
in your post u said you were asked to count the chars alike in the string..did u mean same continuously from the first char.
return 0;
Reply With Quote  
Join Date: Mar 2006
Posts: 38
Reputation: prof.thakur is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
prof.thakur prof.thakur is offline Offline
Light Poster

Re: C problem in strings and recursive check

  #6  
Apr 2nd, 2006
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
Reply With Quote  
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation: YoTaMiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Help Re: C problem in strings and recursive check

  #7  
Apr 3rd, 2006
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
Attached Files
File Type: cpp HW1_Q1.CPP (878 Bytes, 2 views)
Reply With Quote  
Join Date: Feb 2006
Location: India
Posts: 53
Reputation: HackWizz is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: C problem in strings and recursive check

  #8  
Apr 3rd, 2006
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;
Reply With Quote  
Join Date: Feb 2006
Location: India
Posts: 53
Reputation: HackWizz is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: C problem in strings and recursive check

  #9  
Apr 3rd, 2006
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 tried the code using ++s1 ..it will run..
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;
Reply With Quote  
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation: YoTaMiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Re: C problem in strings and recursive check

  #10  
Apr 3rd, 2006
thanx , that will suffice :-) cheers!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:35 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC