VMWare Inc Interview Question
Software Engineer / Developersvoid wordrev(char * str) {
char * last = str;
char * ptr = str;
strrev( str );
while( *ptr != 0 ) {
if ( *ptr == ' ' ) {
*ptr = '\0';
strrev( last );
*ptr = ' ';
last = ptr + 1;
}
ptr++;
}
strrev(last);
return;
}
void strrev(char * str) {
int len = strlen(str);
int len_2 = len / 2;
char temp;
int i;
for(i = 0; i < len_2;i++) {
temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
return;
}
#include <stdio.h>
char sentence[] = "This is my big beautiful sentence having multiple words";
void reverse(char *s)
{
char *tmp = s;
while(s && *s && *s != ' ')
s++;
if(*s == ' ') {
*s = '\0';
reverse(++s);
}
printf("%s ",tmp);
}
int main(void)
{
reverse(sentence);
printf("\n");
return 0;
}
In C# its easy
Take the sentence into string.Then store each word of the string in string array by performing string.split(' ') then apply a decrement loop on this string array and append each word using string builder.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 int
6 main ()
7 {
8 char str[] = "This is a nice world";
9 int len = strlen ( str );
10 int pos = len;
11 char *rev = (char *)malloc( len + 1 );
12 memset ( rev, ' ', len );
13 rev [len] = '\0';
14 printf ("rev = X%sX\n", rev );
15 char *str1 = strtok ( str, " " );
16 len = strlen ( str1 );
17 strncpy ( rev + pos - len, str1, len );
18 pos = pos - len - 1;
19 while ( (str1 = strtok ( NULL, " " )) != NULL )
20 {
21 len = strlen ( str1 );
22 strncpy ( rev + pos - strlen ( str1 ), str1, strlen (str1));
23 pos = pos - strlen ( str1 ) - 1;
24 }
25
26 printf ("rev = %s\n", rev );
27 free ( rev );
28 return 0;
29 }
If you are not allowed to use any library functions to split the string, nor to create any additional strings and/or arrays, in Java you can do it in O(n) by reversing the whole string first and then reversing the words. Here is a solution:
public class WordReversing {
public static char[] reverse(char[] string) {
reverseChars(string, 0, string.length - 1); // reverse whole string
int wordStart = 0;
int wordEnd;
while (wordStart < string.length) {
// skip multiple spaces and find the next word start position
while (wordStart < string.length && string[wordStart] == ' ') {
wordStart++;
}
wordEnd = wordStart;
while (wordEnd < string.length && string[wordEnd] != ' ') {
wordEnd++;
}
reverseChars(string, wordStart, wordEnd - 1); // reverse the word
wordStart = wordEnd + 1;
}
return string;
}
public static void reverseChars(char[] string, int from, int to) {
if (from < 0 || to >= string.length || from >= to) {
return;
}
while (from < to) {
char temp = string[from];
string[from] = string[to];
string[to] = temp;
from++;
to--;
}
}
public static void main(String[] args) {
char[] s = " This is a test. ".toCharArray();
System.out.println(reverse(s));
}
}
<pre lang="java" line="1" title="CodeMonkey75291" class="run-this">public class WordReversing {
public static char[] reverse(char[] string) {
reverseChars(string, 0, string.length - 1); // reverse whole string
int wordStart = 0;
int wordEnd;
while (wordStart < string.length) {
// skip multiple spaces and find the next word start position
while (wordStart < string.length && string[wordStart] == ' ') {
wordStart++;
}
wordEnd = wordStart;
while (wordEnd < string.length && string[wordEnd] != ' ') {
wordEnd++;
}
reverseChars(string, wordStart, wordEnd - 1); // reverse the word
wordStart = wordEnd + 1;
}
return string;
}
public static void reverseChars(char[] string, int from, int to) {
if (from < 0 || to >= string.length || from >= to) {
return;
}
while (from < to) {
char temp = string[from];
string[from] = string[to];
string[to] = temp;
from++;
to--;
}
}
public static void main(String[] args) {
char[] s = " This is a test. ".toCharArray();
System.out.println(reverse(s));
}
}
</pre><pre title="CodeMonkey75291" input="yes">
</pre>
#include <iostream>
#include <stdio.h>
using namespace std;
void reverseString(char* input, int start, int end)
{
if (end - start == 0)
{
return;
}
else if (end - start == 1)
{
char temp = input[start];
char temp2 = input[end];
input[start] = temp2;
input[end] = temp;
return;
}
for(int i = start ; i <= start + (end-start)/2 ; i++)
{
//printf("\r\n swapping %d and %d",i , (start + end - i));
char temp = input[i];
char temp2 = input[start + end - i];
input[i] = temp2;
input[start + end - i] = temp;
}
}
void reverseWords(char* input, int length)
{
int start = 0;
for(int i = 0 ; i <= length ; i++)
{
if (input[i] == ' ' || input[i] == '\0')
{
reverseString(input, start, i-1);
start = i + 1;
}
}
reverseString(input,0, length-1);
}
String reverse(String myString)
{
StringBuilder sb = new StringBuilder(myString);
String rev=sb.toString();
StringTokenizer st = new StringTokenizer(rev);
sb.delete(0,sb.length());
while(st.hasMoreTokens())
{
sb.append(new StringBuilder(st.hasMoreTokens).reverse());
sb.append(" ");
}
return sb.toString();
}
private static String swap(String stringToSwap) {
String[] arr = testStr.split(" ");
int endIndex = arr.length - 1;
int j = endIndex;
String temp = "";
for (int i = 0; i < endIndex / 2; i ++){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j --;
}
StringBuilder sb = new StringBuilder();
for(int k = 0; k < arr.length; k ++){
sb.append(arr[k]);
sb.append(" ");
}
return sb.toString();
}
- Vir Pratap Uttam May 12, 2015