Interview Questions

Given a string S of words and no of character per line m ...

Microsoft Interview Questions and Answers


(Continued from previous question...)

20. Given a string S of words and no of character per line m ...

Question
Given a string S of words and no of character per line m ,with m being greater than the longest word in S,print S in a set of lines so that each line contains no more than m characters and no word split between 2 lines.


maybe an answer1:
The idea is that you need to keep checking for the word length each time you read a word and see if the remaining length of characters that can fit in the line is greater than the word length and print it else you need to print the characters tpo next line.

I am giving pseudo code:
int remLength = m;
String[] str = S.split(" ")
; String tempStr = null;
for(i=0; i<str.length; i++)
{
tempStr = str[i];
if(tempStr.length <=remLength)
{
System.out.print(tempStr);
remLength -= tempStr.length;
}
else
{
i--;
remLength = m;
System.out.println();
}
}



maybe an answer2:
1. The best way would be implementing our own buffer that overflows whenever the size reaches 'm' (of course handling any partial words). If that is too much for the problem the we can write a loop that does the processing like:

const char* givenString = "Align S words in lines where each line can contain at most m characters per line. Words should never be split between two lines.";

std::ostringstream wordBuffer;
std::ostringstream lineBuffer;
std::ostringstream lines;
const char *currentChar = givenString;
const int m = 10;


int countWordsPerLine = 0;
while (('\0' != *currentChar) && (0 != m))
{

wordBuffer << *currentChar;
if(' ' == *(currentChar+1) || ' ' == *(currentChar))
{ //push a complete word
lineBuffer << wordBuffer.str().c_str();
wordBuffer.clear();
wordBuffer.str(std::string());
}
if(m <= lineBuffer.tellp() ||
m <= lineBuffer.tellp() + wordBuffer.tellp())
{ // move to next line
lines << lineBuffer.str().c_str() << "::";
lineBuffer.clear();
lineBuffer.str(std::string());
}
++currentChar;
}
lines << wordBuffer.str().c_str();
std::cout << lines.str().c_str();



maybe an answer3:
/*
Test Input:
I am a final year BE student.
9
Output:
I am a
final
year BE
student.
*/
import java.util.Scanner;
import java.util.Vector;
class Test{
String inpStr;
int aInt;
private Vector<String> aVecStr = new Vector<String>();
Test(int mm, String ss){
inpStr = ss;
aInt = mm;
}
public void convert(){
String[] words = inpStr.split(" ");
String str = new String("");
for(String tmp: words){
if(str.length() + tmp.length() > aInt){
aVecStr.add(str);
str = "";
}
str += tmp+" ";
}
if(str.length()>0)
aVecStr.add(str);
}
public void print(){
for(String s: aVecStr){
System.out.println(s);
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
Test t = new Test(9,"I am a final year BE student.");
t.convert();
t.print();
}
}



maybe an answer3:
include<stdio.h>
#include<conio.h>
void main(){
clrscr();
char *input="hello there is a microsoft";
int m=10;
int rem=0;
int *index=new int[50];
int indexer=0;
for(int i=0;input[i]!='\0';i++){
if(input[i]==' '){
index[indexer++]=i;

}
}
int count=0;
for(int j=0;input[j]!='\0';j++){
if(input[j]==' '){
count++;
if((index[count]-j)+rem>m){
printf("\n");
rem=0;
continue;

}
}
printf("%c",input[j]);
rem++;
}
getch();

}

(Continued on next question...)

Other Interview Questions