Interview Questions

149. paragraph is given find out the second largest word in given space and time.

Microsoft Interview Questions and Answers


(Continued from previous question...)

149. paragraph is given find out the second largest word in given space and time.

Question:
paragraph is given find out the second largest word in given space and time.


maybe an answer:


char* findSecLargest (string const &str) {
char * cstr = (char *) malloc ((str.length()+1) * sizeof(char));
strcpy (cstr, str.c_str());
char * largest = "";
char * secLargest = "";
char * p = strtok (cstr, " .,\?!\"\'");
while (p != NULL) {
if (strlen(p)>strlen(largest)) {
secLargest = largest;
largest = p;
}
else if (strlen(p)>strlen(secLargest)) {
secLargest = p;
}
p = strtok (NULL, " .,\?!\"\'");
}
return secLargest;
}

(Continued on next question...)

Other Interview Questions