Qualcomm Interview Question
Software Engineer / Developers#include <stdio.h>
#include <string.h>
void removeSubstring(const char* str1, const char* str2, char* out);
void removeSubstring(const char* str1, const char* str2, char* out){
if(!str1 || !str2)
return;
int len1 = strlen(str1);
int len2 = strlen(str2);
int out_index=0;
int i = 0; // iterate str1
int j = 0; // interate str2
int count = 0;
int out2= 0;
while (i < len1) {
j=0;
if(str1[i] != str2[j])
out[out_index++] = str1[i++];
else {
out2 = out_index;
for(count=0;j<len2 && i<len1;count++){
if (str1[i] == str2[j++]) {
out[out_index++] = str1[i++];
}
}
if(count==len2){
out_index = out2;
}
}
}
out[out_index]='\0';
}
int main (int argc, const char * argv[]) {
char str1[100]={0};
char str2[10]={0};
char out[100]={0};
strcpy(str1, "HelloWorld");
strcpy(str2, "llo");
removeSubstring(str1, str2, out);
printf("in:%s\t\t out:%s\n", str1, out);
strcpy(str1, "HelloWorld, hello, hello_");
removeSubstring(str1, str2, out);
printf("in:%s\t\t out:%s\n", str1, out);
strcpy(str1, "Hi World!");
removeSubstring(str1, str2, out);
printf("in:%s\t\t out:%s\n", str1, out);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str1[]="hello pravin";
char str2[]="hello";
int i=0,j=0,k=0,flag=0, start=0, end=0;
for(i=0;i<strlen(str1);i++)
{
if(str1[i]==str2[j] && flag==0)
{
start=i;
flag=1;
k++;
j++;
}
else if(str1[i]==str2[j] && flag==1)
{
k++;
if(k==strlen(str2)){
end=i;
}
j++;
}
else{printf("\n here");
flag=0;
k=0;
j=0;
}
}
flag=0;
j=0;
printf("\n %d,%d",start,end);
for(i=0;i<strlen(str1);i++)
{
if(i==start)
{
j++;
str1[i]=str1[end+j];
flag=1;
}
else if(flag==1)
{
j++;
str1[i]=str1[end+j];
}
else if(j==(strlen(str1)-end))
{
str1[i]='\0';
}
}
for(i=0;i<strlen(str1);i++)
{
printf("%c",str1[i]);
}
}
#include <stdio.h>
void remove_occ(char *, char *);
int main()
{
char str1[20] = "Hello World";
char str2[20] = "llo";
//printf("%s",str1);
remove_occ(str1, str2);
printf("%s",str1);
return 0;
}
void remove_occ(char *str1, char *str2)
{
int count1=0, count2=0;
int temp=0, count1_temp;
while(str1[count1]!='\0')
{
count1_temp=count1;
while(str1[count1] == str2[count2])
{
count1++;
count2++;
}
if(str2[count2] != '\0')
count1=count1_temp;
str1[temp++] = str1[count1++];
count2=0;
}
str1[temp]='\0';
}
Look at this, this order of n.
#include<stdio.h>
#include<string.h>
void StrRemove(char* Src,char *Rstr);
main()
{
char Str[]="I am Vijay Kumar Immadi";
char Str2[]="Vijay";
StrRemove(Str,Str2);
printf("\n%s",Str);
// return 0;
}
void StrRemove(char *Src,char *Rstr)
{
int len1,len2;
int i,j,true,current;
len1=strlen(Src);
len2=strlen(Rstr);
i=0;j=0;true=0;current=0;
while(Src[i]!='\0')
{
Src[current]=Src[i];
if(Src[i]==Rstr[j])
{
j++;
true=1;
}
else if(true)
{
true=0;
j=0;
}
if(j==len2)
{
current=current-j;
j=0;
true=0;
}
i++;
current++;
}
Src[current]='\0';
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *deleteclean(char *,char *);
void main(){
char *s1="aaaaa";
char *s2="cdefag";
char *s3=deleteclean(s1,s2);
printf("%s\n",s3);
}
char *deleteclean(char *s1, char *s2){
char hash[256];
int i;
// hashtable consists of all ASCII characters
for(i=0;i<256;i++)
hash[i]=0;
char *t=s1;
// if a char occurs the hashtbale is updated
while(*t!='\0'){
hash[*t]=1;
t++;
}
int cleanlen=0;
int count=0;
t=s2;
while(*t!='\0'){
if(hash[*t]==1)
count++;
else
cleanlen++;
// All this does is keeps a count of char that occur in s1
t++;
}
cleanlen++; // null terminator
if(cleanlen==0)
return strdup(s2);
// cleanlen is now the length of the string after the char that occur in s1 are removed
// allocates space for a char string of size cleanlen
char *clean=(char *)malloc(cleanlen*sizeof(char));
char *r=clean;
t=s2;
while(*t!='\0'){
if(hash[*t]==0)
*clean++=*t;
t++;
}
*clean='\0';
return r;
}
<pre lang="c" line="1" title="CodeMonkey84633" class="run-this">#include<stdio.h>
#include<string.h>
void remove_char(char *string1,char *string2)
{
char *slow,*fast;
slow=fast=str1;
while(*fast!='\0')
{
if(memcmp(fast,str2,strlen(str2))==0)
fast+=strlen(str2);
else
*slow++=*fast++;
}
*slow='\0';
}
int main(int argc,char **argv)
{
char str1[256]="HelloWorld";
char str2[256]="llo";
printf("%s\n",str1);
remove_char(str1,str2);
printf("%s\n",str1);
return 0;
}</pre>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);
memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;
for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}
for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}
return -1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);
memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;
for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}
for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}
return -1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);
memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;
for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}
for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}
return -1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);
memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;
for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}
for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}
return -1;
}
Find the code at
- Teja October 21, 2009http://codepad.org/7DP6z2dF
I wrote this without using any string functions other than strlen.. Using some string functions optimizes it however..