Fungible Interview Question
Developer Program EngineersCountry: United States
Interview Type: In-Person
{
{
{
char* printWithRemovedSpace(char* string){
int length = strlen(string);
char* newString = (char *) malloc(sizeof(char) * length);
int i = 0;
int numberOfSpaces = 0;
int j = 0;
for(i = 0; i < length; i++){
if(string[i] != ' '){
newString[j++] = string[i];
numberOfSpaces = 0;
}else if(string[i] == ' '){
if(numberOfSpaces == 0){
newString[j++] = string[i];
numberOfSpaces++;
}
}
}
newString[j] = '\0';
return newString;
}
}
}
}
package com.myjava.string;
import java.util.StringTokenizer;
public class MyStrRemoveMultSpaces {
public static void main(String a[]){
String str = "String With Multiple Spaces";
StringTokenizer st = new StringTokenizer(str, " ");
StringBuffer sb = new StringBuffer();
while(st.hasMoreElements()){
sb.append(st.nextElement()).append(" ");
}
System.out.println(sb.toString().trim());
}
}
char* printWithRemovedSpace(char* string){
- Anonymous March 12, 2017int length = strlen(string);
char* newString = (char *) malloc(sizeof(char) * length);
int i = 0;
int numberOfSpaces = 0;
int j = 0;
for(i = 0; i < length; i++){
if(string[i] != ' '){
newString[j++] = string[i];
numberOfSpaces = 0;
}else if(string[i] == ' '){
if(numberOfSpaces == 0){
newString[j++] = string[i];
numberOfSpaces++;
}
}
}
newString[j] = '\0';
return newString;
}