Interview Questions

205. Reverse words in a string

Microsoft Interview Questions and Answers


(Continued from previous question...)

205. Reverse words in a string

Question:
Reverse words in a string


maybe an answer:


void reverse(char *k)
{
while(*k)
{
int a = reverseword(k);
k += a;
if(*k)
k++;
}
}
int reverseword(char *s)
{
int start = 0,length = 0;
char *p = s;
while(*p != ' ' && *p != ',' && *p != '\0')
{
length++;
p++;
}

for(int i=start,j = length-1;i<j;i++,j--)
{
char c = s[i];
s[i] = s[j];
s[j] = c;
}
return (length - start);
}

(Continued on next question...)

Other Interview Questions