Qualcomm Interview Question
Software Engineer / Developersint cnt1 = 0, cnt2 = 0;
for(int cnt1 = 0; str[cnt1]!='\0'; cnt1++)
{
if (str[cnt1] == ' ')
cnt1++;
str[cnt2] = str[cnt1];
}
str[cnt2] = '\0'
The above implementation fails if there are two spaces together, something like "Hi there wassup" , there are two spaces after "there".
#include<stdio.h>
#include<string.h>
int main()
{
char s[]="hi guys wassup";
int i=0,j=0;
while(a[j]!='\0')
{
if(s[j]!=' ')
s[i++]=s[j];
j++;
}
s[i]='\0';
printf("%s\n",s);
return 0;
}
#include <stdio.h>
int main(void)
{
char str[]="aaaaa bbbbbbbbbbbb ccccccccccccc";
int read, write;
read = write = 0;
while(str[read] != '\0')
{
while(str[read] != ' ' && str[read] != '\0')
str[write++] = str[read++];
if(str[read] == ' ')
str[write++] = str[read++];
while(str[read] != '\0' && str[read] == ' ')
str[read++];
}
str[write] = '\0';
printf("%s",str);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
char* remove_spaces(char *str)
{
char *ptr2;
ptr2=str;
while(*str !='\0')
{
if(*str == ' ')
{
*str = ++*str;
str++;
}
else
{
str++;
}
}
return ptr2;
}
int main()
{
char s[100];
printf("\n Enter the String:")
gets(s);
char *ans;
ans = remove_spaces(s);
printf("\n The string without spaces is %s",ans);
return 0;
}
IMO this is what they are looking for, more often than not QC is more interested in your C skills rather than algorithmic skills.
char a[] = "Hi there wassup Hi there wassup";
int len = 31, i=0;
while( i < len)
{
if(a[i] == ' ')
{
memmove( (void*)&a[i], (void*)&a[i+1], len - i+1);
}
i++;
}
- emge October 26, 2009