Microsoft Interview Question






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

void truncate(char str[]){

int read=0,write=0;

//one extra for NULL, this is the max size if no extra spaces found
char buffer[strlen(str)+1];

while(str[read]){
if(str[read++] == ' '){
buffer[write++]=' ';
while(str[read]== ' ')
read++;
}
else{
buffer[write++]=str[read++];
}
}
buffer[write]='\0';
strcpy(str,buffer);
}

- Raman November 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i have corrected bugs in the above code...

void truncate(char str[]){

 int read=0,write=0;
 //one extra for NULL, this is the max size if no extra spaces found
 char buffer[strlen(str)+1];

 while(str[read]){
   if(str[read] == ' '){
     buffer[write++]=' ';
     while(str[read] && str[read]== ' '){       
       read++;
	 }
   }
   else{
     buffer[write++]=str[read++];
   }
 }
 buffer[write]='\0';
 strcpy(str,buffer);
}

- Raman November 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void truncate(char* str){
	char* res;
	int i=0,j=0;
	int flag=1;
	while(str[i]){
		if(str[i]=' ' && flag=1){
			res[j++]=str[i++];
			flag=0;
		}
		else{
			res[j++]=str[i++];
			flag=1;
		}
	}
}

- Vamsi December 05, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* truncateExtraSpaceBetweenWords( char* str )
{
  if (!str) return NULL;
  if (!*str) return str;
  char* ans = str;
  int i = 0, j = 0;
  
  while (1)
  {
    while(str[i] && str[i] == ' ') // remove all leading spaces
      i++;

    if (!str[i])
    {
      if (j == 0)str[j++] = ' ';
      else if (str[j-1] == ' ') j--; // remove tailing spaces
      str[j] = '\0';
      return ans;
    }

    // move forward till you find non-space characters
    while(str[i] && str[i] != ' ')
    {
      str[j++] = str[i++];
    }
    if (!str[i])
    {
      str[j] = '\0';
      return ans;
    }
    else
    {
      str[j++] = str[i++]; // = ' '
    }
  }
}

- Raaj February 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just function...strtok suffices the requirement.

- possible June 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char str[] ="This a sample string . ";
char *pch,*s;
pch = strtok (str," ");
while (pch != NULL)
{
concat(s,pch);
pch = strtok (NULL," ");
}
cout<<s;

- sg December 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.careercup;



public class TruncateConsecutiveSpaces_2991 {
	public static void main(String[] args){
		String stringToRidOfSpaces = "";
		StringBuilder outputString = new StringBuilder();
		stringToRidOfSpaces = "abc   abc def";
		int counter = 0;
		char lastChar = '\0';
		while(counter<stringToRidOfSpaces.length()){
			char currentChar = stringToRidOfSpaces.charAt(counter);
			if(lastChar == ' ' &&  currentChar == ' '){
				System.out.println("Getting rid of this");
			} else {
				outputString.append(currentChar);
			}
			lastChar = currentChar;
			counter++;
		}
		System.out.println(">>"+outputString+"<<");
	}
}

- Ankit Kinra September 24, 2012 | 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