HCL America Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

char* substrrecursion(char* str, char*s)//str is the string and s is the substring
{ int k=0,i=0,j=0,m=0,t=0;
char* str1;
for(i=0;i<strlen(str);i++)
{if(str[i]==s[k]){
t++;
for(int j=1;j<strlen(s);j++)
{if(str[i+j]==str[++k]){m++
continue;}
else
break;
}
}}
if(m==strlen(s)-1)
{
for(j=0,k=0;k<strlen(str);j++){
if(j!=i){
str1[j]=str[k];
k++;
}
else(k=i+strlen(s))
}}
substrrecursion(str1,s);
}
else{ return str1;
}
}

- matrix September 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above is way too complicate - using strstr makes it a lot easier:

void remsub(char* str, const char* pattern)
{
	char* loc = NULL;
	int len_pat = strlen(pattern);
	while(loc = strstr(str, pattern)) {
		strcpy(loc, loc+len_pat);
	}
}



int main()
{
	char val[] = "Hello, xyyddccyydcc";
	const char* pattern = "yd";
	remsub(val, pattern);
	printf("%s\n",val);

	return 0;
}

This gives you:

Hello, xccycc

- mmzymxf October 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here In While loop there is no change in Pattern and str so when while loop will going to stop
can u plz explain the login behind this

- Dnyaneshwar November 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Dnyaneshwar, the pattern doesn't change (and shouldn't). The string is changed by the strcpy in the while loop -- 'loc' points to the beginning of the pattern within the original string.

While I agree that the strstr solution is a lot simpler than the preceding one, it is not a recursive one as required. Here is a recursive (and slower) version:

// recursive version
void remsub(char* str, const char* pattern)
{
    char* loc = strstr(str, pattern);
    if (!loc)
        return str;
    else {
        strcpy(loc, loc+strlen(pattern));
        return remsub(str, pattern);
    }
}

- MartinM December 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops, there's a small mistake in the recursive version above I submitted, the function definition should have been:

char* remsub(char* str, const char* pattern)

The main() program would also have to be slightly changed to accommodate this modification.

- MartinM December 31, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

to Dnyaneshar: if you add one line to the code and check the output, you will understand
it better.

void remsub(char* str, const char* pattern)
{
char* loc = NULL;
int len_pat = strlen(pattern);
while(loc = strstr(str, pattern)) {
puts(loc);
strcpy(loc, loc+len_pat);
}
}



int main()
{
char val[] = "Hello, xyyddccyydcc";
const char* pattern = "yd";
remsub(val, pattern);
printf("%s\n",val);


return 0;
}

- vivian November 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Dhyaneshwar.
The while loop will end when strstr() returns NULL to loc i.e. There are no more patterns found.

- Anonymous November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

char *rmv(char *src,const char *ptr){
char *first;
first=strstr(src,ptr);
if(!first)
return src;
else{
cout<<first<<endl;
strcpy(first,first+strlen(ptr));
rmv(src,ptr);
}
return src;
}


int main(){
char s[]="aabbccddccddcc";
const char *p="cc";
char *t;
t=rmv(s,p);
cout<<t;
getchar();
return 0;
}

- ridercoder October 19, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More