Amazon Interview Question
Software Engineer / Developerschar * findstr(const char * str)
{ char *p;
while(*str)
{
if(*str=='0')str++;
elseif
(*ptr)++=(*str)++;
}
return p;
}
OOOOps...........sorry ptr and p is same char pointer........its a just a spelling mistake...
The question asks to retain single space, not completely rip them off
void Squeeze(char *str){
if(str==NULL)
return;
int i=0,index=0;
while(str[i]!='\0'){
while(str[i]!=' ' && str[i]!='\0'){
str[index]=str[i];
index++;i++;
}
str[index++]=str[i++];
while(str[i]==' ' && str[i]!= '\0')
i++;
}
str[index]='\0';
}
i am new to programming...i've a question for hopebasedcoder..can you please check again ur solution where
index++;i++;
}
str[index++]=str[i++];
check against "abc " if u can produce "abc"..m i missing something?
public class DeleteSpacesFromAString {
public static void main(String args[]) {
String s = "fffg f f f f f f ffff ff";
boolean flag = true;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ' && flag) {
flag = false;
sb = sb.append(s.charAt(i));
} else if(s.charAt(i) != ' ')
{
flag = true;
sb = sb.append(s.charAt(i));
}
}
System.out.println(sb.toString());
}
}
2 ptrs. 1 read - another write.
- lord Darth Plagueis June 29, 2009Read is faster than write. read reaches EOString - write a \0 in the write ptrs location.