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!=s2) return 0;
if (s1==s2) return 1+func(s1[i+1],s2[i+1]);
}

am i missing something ?

thank you

Yotam :D

Recommended Answers

All 9 Replies

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!=s2) 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

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!=s2) 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

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.

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.

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

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 :D

I am putting up the code in attachment.... i tried watching the process and i still cant find whats wrong

thanx :D

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

I am putting up the code in attachment.... i tried watching the process and i still cant find whats wrong

thanx :D

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

thanx , that will suffice :-) cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.