Whats wrong with my code! Microsoft didn't continue interview after reviewing my code !! please help
I only looked at your first solution, and let me tell you: you _NEED_ to use better variable names, and you need to use more comments. How many of those are 1 letter variables, and if I didn't know what the knuth-morris-pratt algorithm was, how would I know you were doing? No comments makes work on MY end in order to decipher what you are doing - if I am a hiring manger, I want to be able to understand exactly what you are doing without having to use a Rosetta Stone to interpret your code. During on-site interviews less comments are OK cause we are talking though it and I see your thinking as you are writing. When you turn this in and I have to read it so you need to explain your thoughts.
Here is what I came up with strstr(). Hope this helps to others:
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}
Here is what I came up with strstr(). Hope this helps to others:
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}
-------------------------------------------------
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}
-------------------------------------------------
-------------------------------------------------
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}
-------------------------------------------------
-------------------------------------------------
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}
-------------------------------------------------
-------------------------------------------------
char* strstr1(char* str, char* substr)
{
char* tmpstr = str;
char* tmpsubstr = substr;
char* resultstr=NULL;
bool inmatch = false;
while((*tmpstr!= '\0')) {
if(*tmpstr == *tmpsubstr) {
resultstr = tmpstr;
inmatch = true;
do {
if(*tmpstr != *tmpsubstr) {
inmatch = false;
break;
}
tmpstr++;
tmpsubstr++;
} while(*tmpstr!= '\0' && *tmpsubstr != '\0' );
if((*tmpsubstr == '\0') && (inmatch == true)) {
return resultstr;
} else {
tmpsubstr = substr;
resultstr = NULL;
continue;
}
}
tmpstr++;
}
return resultstr;
}
-------------------------------------------------
Hi Kalpesh,
- Samrat Som May 18, 2008just for the sake of info. I did the prog but it is in java.Taken a HashTable,which will keep the track of character with the funda of collision hashing.Here it goes.
public class DuplicateArr {
public static void main(String[] args) {
String test="nickname";
String orig="";
test=test.toUpperCase();
int temp;
char[] test1=test.toCharArray();
int[] hash= new int[26];
for(int i=0;i<test.length();i++)
{
temp=test1[i]-65;
if (hash[temp]==0)
{
orig+=test1[i];
hash[temp]=1;
}
}
System.out.println("The String without Duplicate: "+orig);
}
}