Tuesday, July 14, 2009

C++: How to find the difference of 2 strings ?

For ex:


char * message = "Please answer this for me will be thankful" ;


char * substr = "Please answer this";





diff (message, substr) ;


should return "for me will be thankful";





Thanks in advance.





-Mazin

C++: How to find the difference of 2 strings ?
Here's diff





diff(char *message,char *substr)


{


/*hope the first few characters are equal*/


int len[2];


int i,l;





len[0]=strlen(message);


len[1]=strlen(substr);





if(len[0]%26lt;len[1])


{


i=1;


l=len[0];


}


else


{


i=0;


l=len[1];


}





while(l%26lt;=len[i])


{


printf("%c",*(message+l));


l++;


}


}
Reply:if you're asking if there is a function that can do that the answer is no. I'm afraid you'll will have to develope it. As I see it, seems easy. For more info klastor00@yahoo.com anytime
Reply:char *diff(char* a, char *b) {while (*a++ %26amp;%26amp; *b++); return *a ? a : b;}
Reply:rtfm!
Reply:You will have to create it yourself I think. I will guide you through it, but not supply the code.





Make a function, as you described, diff(message, substr)


Inside, the function, start a loop that stops when the first null character, signalling the end of the string, is found.


Keep a counter of how many times the loop looped.


This counter will be a position in the message for where the substr ends.


Return or print out the required substring " for me will be thankful", it should have a space at the start, as the other does not have a space at the end.


No comments:

Post a Comment