Microsoft Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
Agreed.
#include <cstdlib>
#include <cstring>
#include <iostream>
void reverse(char *src, char *end)
{
while(src < end) {
char temp = *src;
*src++ = *end;
*end-- = temp;
}
}
void reverse_sentence(char *src, size_t len)
{
if(len < 3) {
return;
}
reverse(src, src + len - 2);
char *end = src + len - 1;
while(src != end) {
char *start = src;
for(; start < end && *start != ' '; ++start)
;
reverse(src, start - 1);
for(;start < end && *start == ' '; ++start)
;
src = start;
}
}
int main()
{
char c[] = "hey there world.";
std::clog << "before reversing " << c << std::endl;
reverse_sentence(c, std::strlen(c));
std::clog << "after reversing " << c << std::endl;
return (EXIT_SUCCESS);
}
There is no need of additional data structures, it can be done in place. In your approach u need to create words from the sentence and then place it into stack. So much effort may not be required. We can reverse the complete sentence and then reverse every word in the output of the sentence reversal step.
In-place implementation of the reverse sentence words. Algorithm description inlined in s/c.
#include <stdio.h>
#include <string.h>
void rev_string(char *dat, int len)
{
char *ps, *pe, tmp;
if (1 == len)
return;
ps = dat; // the start of the data
pe = dat + len - 1; // the end (last char) of the data
while (ps < pe) {
tmp = *ps;
*ps = *pe;
*pe = tmp;
ps++;
pe--;
}
}
/**
Do an in-place reversing of the words in sentence,eg.
"1234 56 78 9" -> "9 78 56 1234"
Algorithm:
1. Reverse the whole string
2. Scan the resulting sentence from left to right and:
a) Find each token (word delimited by whitespace)
b) Reverse each token
c) Loop back to a) while there are more tokens
*/
void rev_sentence_words(char dat[], int len)
{
int i, j;
char *ps;
rev_string(dat, len);
for (i = 0; i < len; i++) {
if (dat[i] == ' ')
continue;
j = i;
ps = &dat[j];
/* Find the next whitespace */
while (dat[i] && dat[i] != ' ')
i++;
rev_string(ps, i - j);
}
}
int main(int argc, char* argv[])
{
char dat[] = " the careercup just rocks ";
printf("Before: <%s>\n", dat);
rev_sentence_words(dat, strlen(dat));
printf("After: <%s>\n", dat);
return 0;
}
void reverse(char* start, char* end)
{
while (start < end)
{
char tmp = *end;
*end = *start;
*start = tmp;
start++;
end--;
}
};
int main()
{
char str[] = "I love you";
char* start = str;
char* end = str + 1;
while(end <= start + 10)
{
if(*end == '\0' || *end == ' ')
{
reverse(start, end - 1);
start = end + 1;
}
end++;
}
reverse(str, str + 9);
printf("%s", str);
return 0;
}
#include<iostream>
#include<string>
void reverse(char *a, int i, int j)
{
if(i>j)
{
int temp=i;
i=j;
j=temp;
}
char temp;
while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
}
void reversesentence(char * a, int length) //assumption
{
int i=0;
int j=0;
while(j<length-1)
{
while(a[j]==' '&&j<length-1)
{
j++;
i=j;
}
while(a[j]!=' '&&j<length-1)
{
j++;
}
if(a[j]==' ')
{
j--;
}
reverse(a,i,j);//first blank
j++;
i=j;
}
reverse(a,0,length-1);
}
void main()
{
char a[]=" i am lucky ";
reversesentence(a,strlen(a));
puts(a);
}
char str[] = "Hello World";
char str2[] = "";
int i;
for(i=strlen(str)-1; i<=0; i--)
{
if(str[i]==' ')
{
strcat(str2,str+i+1);
strcat(str2," ");
str[i]=0; // cut
}
}
// first that becomes last
strcat(str2,str);
printf("\n str: %s", str2);
Reverse words, reverse sentance algo is great.
Here is another:
Time: O(2n)
Space: (On)
private static char[] ReverseWordsInSentance(char[] input)
{
if (input == null || input.Length < 2) return input;
var output = new char[input.Length];
var outputRunner = 0;
var latestLimit = input.Length;
for(int i = input.Length - 1; i >= -1; --i)
{
if(i==-1 || input[i] == ' ')
{
for (int j = i + 1; j < latestLimit; ++j)
{
output[outputRunner++] = input[j];
}
if(i != -1)
{
latestLimit = i;
output[outputRunner++] = ' ';
}
}
}
return output;
}
import java.util.ArrayList;
public class StringToStringArray {
/**
* @param args
*/
public static void main(String[] args) {
char[] a = "Hello World How are you".toCharArray();
ArrayList<String> myArrayList = new ArrayList<String>();
boolean isFinalCharSpace = false;
//get String array from Char array
StringBuilder sb = new StringBuilder();
int j = 0;
for (int i = 0; i < a.length; i++) {
isFinalCharSpace = false;
if (a[i] != ' ') {
sb.append(a[i]);
} else if (a[i] == ' ') {
if (sb.length() > 0) {
myArrayList.add(sb.toString());
} else {
myArrayList.add(" ");
}
j++;
sb.setLength(0);
isFinalCharSpace = true;
}
}
if (!isFinalCharSpace) {
myArrayList.add(sb.toString());
j++;
sb.setLength(0);
}
int length = myArrayList.size();
//swap the corresponding elements
for (int k = 0; k < length / 2; k++) {
String temp = myArrayList.get(k);
myArrayList.set(k, myArrayList.get(length - 1 - k));
myArrayList.set(length - 1 - k,temp);
}
}
}
Reverse the whole sentence and then reverse the output word by word
CODE :
void reverseWordByWord (char *str) {
char *start = str;
int len = strlen(str);
char *end = str+len-1;
reverse(start, end);
int i;
for (i=0; i<len; i++) {
if (str[i]== ' ') {
end = &str[i-1];
reverse(start,end);
start = &str[i+1];
}
}
//Reverse the last word !
end = &str[i-1];
reverse(start,end);
}
Reverse the individual strings before the white space
- coding for fun February 28, 2012Example :
I am boy
I ma yob
after reverseral of each individual string reverse the entire string
o/p
boy am I
Example 2
Hello Life
a) olleH efiL
b) Life Hello
Note :
In place algorithm..No extra space needed...