Microsoft Interview Question
// first case
#include<stdio.h>
#include<string.h>
void rev(char * str, int i, int j)
{
char temp;
while(j != i + 1 && j != i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j--;
i++;
}
}
int main()
{
int strLen = 0;
char str[100];
int i = 0, j = 0 ;
strcpy(str , "a quick brown fox");
strLen = strlen(str);
for(i = 0; i < strLen; )
{
j = i;
while(str[j] != ' ' && str[j] != '\0')
{
j++;
}
// printf("%s, %d, %d\n", str, i, j);
rev(str, i, j - 1);
i = j + 1 ;
}
printf("\n%s\n", str);
return 1;
}
// second case ','
#include<stdio.h>
#include<string.h>
void rev(char * str, int i, int j)
{
char temp;
//printf("rev - %s, %d, %d\n", str, i, j);
while(j != i + 1 && j != i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j--;
i++;
}
}
int main()
{
int strLen = 0;
char str[100];
int i = 0, j = 0 ;
strcpy(str , "a quick, brown fox");
strLen = strlen(str);
for(i = 0; i < strLen; )
{
j = i;
while(str[j] != ' ' && str[j] != ',' && str[j] != '\0')
{
j++;
}
//printf("%s, %d, %d\n", str, i, j);
rev(str, i, j - 1);
while(str[j] == ' ' || str[j] == ',' || str[j] == '\0')
{
j++;
}
i = j;
}
printf("\n%s\n", str);
return 1;
}
looking for space and replacing the word without special letters like:
!,?"()[]{}
char newStr = new(strlen(sentence))
char *start = sentence
char *ptr = sentence
while (ptr!=NULL) && !(isspace(ptr)) ptr++;
if ptr == NULL
copyFromStartToPtr(start, ptr) //copy includes the '\0'
else
int strtIdx=0,endIdx=0;
while (!check(start+strtIdx) && ((start+strtIdx)!=ptr)) strtIdx++;
while (!check(ptr-endIdx)&&
((ptr-endIdx)!=ptr) &&
((start+strtIdx)!=ptr)) ) endIdx--;
if ((start+strtIdx)!=ptr))
switchTheWord(start+strtIdx, ptr-endIndx,newStr);
// copying the special characters
while (strtIdx)
{
// copy the special char
*(newStr+(start-sentence)+strtIdx)=*(ptr+strtIdx)
strtIdx--;
}
while (endIdx)
{
// copy the special char
*(newStr+ptr-start+endIdx)=*(sentence+ptr-start+endIdx)
endIdx--;
}
switch like in the previous code
check should check that the word start and ends with letters
hope its clear.
:)
Note the following code has various problems. Just to give you a general idea.
1. char* input = "This is "
"This is " is a constant so that the letters can not be changed. However,
char input[] = "This is ", the letters can be changed.
============================================
int main(){
..char* input = "This is a good day!";
..char* wordBegin;
..char* wordEnd;
..char tmp;
..char* output=input;
..while (input!='\0'){
....wordBegin=input;
....wordEnd=input;
....//step1: find begin of word
....while ((!(((*wordBegin)>='A') && ((*wordBegin)<='Z') || ((*wordBegin)>='a') && ((*wordBegin)<='z')))&& (wordBegin!='\0'))
......wordBegin++;
....//step2: find end of word
....wordEnd=wordBegin;
....while (((*(wordEnd+1)>='A') && ((*(wordEnd+1)<='Z')) || ((*(wordEnd+1)>='a') && ((*(wordEnd+1))<='z'))))
......wordEnd++;
....input=wordEnd;
....//step3:swap letter by letter
....while (wordBegin<wordEnd){
......tmp=*wordEnd;
......//*wordEnd=*wordBegin;
......*wordBegin=tmp;
......wordBegin++;
......wordEnd--;
....}
......if (input!=0)
........input++;
..}
}
Here is the working code.
-------------------------------------
int main()
{
..string input = "This is a good day !!";
..int wordBegin=0;
..int wordEnd=0;
..int iterator=0;
..char tmp;
..while (iterator<input.size()){
....wordBegin=iterator;
....wordEnd=iterator;
....//step1: find begin of word
....while ((!((input[wordBegin]>='A') && (input[wordBegin]<='Z') || ((input[wordBegin]>='a') && (input[wordBegin]<='z')))&& wordBegin<input.size()))
......wordBegin++;
//step2: find end of word
....wordEnd=wordBegin;
....if (wordEnd<input.size()){
......while ((((input[wordEnd+1]>='A') && ((input[wordEnd+1]<='Z')) || ((input[wordEnd+1]>='a') && ((input[wordEnd+1])<='z')))))
........wordEnd++;
......iterator=wordEnd;
....}
....//step3:swap letter by letter
....while (wordBegin<wordEnd){
......tmp=input[wordEnd];
......input[wordEnd]=input[wordBegin];
......input[wordBegin]=tmp;
......wordBegin++;
......wordEnd--;
....}
....iterator++;
..}
..cout<<input;
}
Here is the better code, copied from
http://www.careercup.com/question?id=62591
=================================================
void rev(char *r,char *l)
{
..char temp;
..while(r<l)
..{
....temp=*r;
....*r=*l;
....*l=temp;
....r++;
....l--;
..}
}
void revword(char *str)
{
..char *end,*x,*y;
..for(end=str;*end;end++);
..//reverse complete sentence
..rev(str,end-1);
..//reverse word in the sentence
..x=y=str;
..while(x++<end)
..{
....if(*x==' '||*x=='\0')
....{
......rev(y,x-1);
......y=x+1;
....}
..}
}
int main()
{
//Note here, char*s does not work, because *a is in read only const area, while s[]is in stack,changeable by rev().
//char *s="my name is anthony";
..char s[]="my name is anthony";
..revword(s);
..printf("\nafter rev : %s",s);
..return 0;
}
Case 1:
- Jack March 24, 2006Swap whole string in-place. Then swap substrings in-place.
Case 2:
Two unsigned integer iterators, left/right. left goes ->. right goes <-.
If A is a comma, increment left.
If B is a comma, increment right.
swap A,B in-place.
Do until right>left.
Case 3:
1. Put a comment about the bug in the CVS explaining in detail what the bug is and what I did.
2. Document the code.
3. Explain my algoritm during a code review.