Monday, May 24, 2010

Can u help me to change this programme to c++?

#include %26lt;stdio.h%26gt;





bool string_smaller_than(char *first, char *second) {


int i=0;


bool bRet = FALSE;





while (first[i] %26amp;%26amp; second[i] %26amp;%26amp; bRet == FALSE) {


if ([i]first %26lt; second[i]) bRet = TRUE;


i++;


}


return bRet;


}





bool string_ptr_smaller_than(char *first, char *second) {


char *pfirst, *psecond;


bool bRet = FALSE;





pfirst = first;


psecond = second;


while (*pfirst %26amp;%26amp; *psecond %26amp;%26amp; bRet == FALSE) {


if (*pfirst %26lt; *psecond) bRet = TRUE;


pfirst++;


psecond++;


}


return bRet;


}





void main(void) {


char first_str[100], second_str[100];





printf("Enter the first string: ");


scanf ("%s",first_str);


printf("Enter the second string: ");


scanf ("%s",second_str);





printf("Using string_smaller_than, result is %s\r\n", string_smaller_than(first_str, second_str) ? "TRUE" : "FALSE");


printf("Using string_ptr_smaller_than, result is %s\r\n", string_ptr_smaller_than(first_str, second_str) ? "TRUE" : "FALSE");


}

Can u help me to change this programme to c++?
If you are still stuck , you may contact me at homework@live.in
Reply:Posting the errors or incorrect answers you are getting can greatly speed up any help you might be able to get.





if ([i]first %26lt; second[i]) bRet = TRUE;


i++;


}


return bRet;


}





should be


if (first[i] %26lt; second[i]) bRet = TRUE;


i++;





you put your indexing in the wrong place.





I take it *pfirst and *psecond is checking for the end of the string. I have not seen it done this way before and am not sure on its correctness.





I am also not sure about the general logic.





Take the strings:





String 1: aabbcc


String 2: bbaacc


String 3: aaaaaa





func(String 1, String 2) will return TRUE


func(String 1, String 3) will return TRUE





String 2 and String 3 do not compare the same way alphabetically to String 1 and so they should not be returning the same answer.





I think what you need to do is also check if *pfirst %26gt; *psecond and return false as soon as that happens.

yu gi oh cards

No comments:

Post a Comment